Is it possible to output text on multiple lines in an OBJ_TEXT object? - page 3

 
Alexey Viktorov:
Nikolay, is it possible to align the text to the top right corner. I've been struggling for half a day... I've aligned it to the right corner, but the text is aligned only to the left side. Can you give me a hint?
I know at least two ways.
Alexei, I'll show you as soon as I get to the computer, if you can't do it before then.
Do you want it on the canvas or on the objects?
 
Nikolai Semko:
I know at least two ways.

set the anchor parameter in TextOut (and probably the object itself as well)

calculate the text size and take it into account )

 
Andrei Trukhanovich:

set the anchor parameter in TextOut (and probably the object itself as well)

calculate text size and take it into account )

right
 
Alexey Viktorov:
Nikolai, is it possible to align the text to the top right corner. I've been struggling for half a day... The right-hand corner is aligned, but the text is only aligned to the left side. Can you give me a hint?
#property indicator_chart_window
#include <Canvas\iCanvas.mqh> //https://www.mql5.com/ru/code/22164

int OnInit()
  {
   RightTopText("Текст в правом верхнем углу","Tahoma",25);
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const int begin,
                const double &price[])
  {
   return(rates_total);
  }
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
                  const long &lparam,
                  const double &dparam,
                  const string &sparam)
  {
   if(id==CHARTEVENT_CHART_CHANGE) RightTopText("Текст в правом верхнем углу","Tahoma",25);
  }
//+------------------------------------------------------------------+
void RightTopText(string str,string name,int size)
  {
   Canvas.Erase();
   Canvas.FontSet(name,size);
   Canvas.TextPosition(W.Width-Canvas.TextWidth(str)-5,0);
   Canvas.Comm(str);
   Canvas.Update();
  }
//+------------------------------------------------------------------+
 
Nikolai Semko:
I know at least two ways.
Alexei, I'll show you as soon as I get to the computer, if you can't do it before then.
Do you want it on canvas or on objects?
On canvas, of course. Since the question is directed to you.
 
Andrei Trukhanovich:

set the anchor parameter in TextOut (and probably the object itself as well)

calculate text size and take it into account )

I tried it. Yesterday I was struggling with it for half a day. As soon as I change something the text disappears completely. And I can't find it.
 
Nikolai Semko:

That's not what I meant. The right-hand corner is fine. But if two or more lines of different length, they are aligned by the first character of the line. And I would like strings to be aligned by the last character of the string.

This is the first line.

This is the second line.

And this is the third line.

In the example on the first page, the second and third parts of the code use pixel shift or percentage shift. But the lines can only be aligned by indenting from the left edge. But this is not always convenient, especially if the length of the string can vary. For example "sum = 10" or "sum = 1000".
 

It's strange that the topic has gone to Canvas. I've been doing this since time immemorial:

string obj_pref="A_EA_";
void Text(int i,string text="",color CLR=clrBlue)
{
   string name=obj_pref+IntegerToString(i);
   if(ObjectCreate(0,name,OBJ_LABEL,0,0,0))
    {
     ObjectSetInteger(0,name,OBJPROP_CORNER,CORNER_RIGHT_UPPER);
     ObjectSetInteger(0,name,OBJPROP_ANCHOR,ANCHOR_RIGHT_UPPER);
     ObjectSetInteger(0,name,OBJPROP_XDISTANCE,5);
     ObjectSetInteger(0,name,OBJPROP_YDISTANCE,int(i*FS*1.4));
     ObjectSetInteger(0,name,OBJPROP_FONTSIZE,FS);
     ObjectSetString (0,name,OBJPROP_FONT,"Arial");
    }
    ObjectSetString (0,name,OBJPROP_TEXT,text);
    ObjectSetInteger(0,name,OBJPROP_COLOR,CLR);
}

FS - font height.

PS example for top right :)

 
Igor Zakharov:

It's strange that the subject has gone to Canvas. I've been doing this since time immemorial:

FS - font height.

PS example for top right :)

That's how I've been able to do it for years too.

 

That's it, I've got it.

We alignOBJ_BITMAP_LABEL object by the right edge, we may indent it horizontally and vertically, we assign OBJPROP_ANCHOR to ANCHOR_RIGHT_UPPER. Then the width of the image is set when creating the resource. And this value should be put in TextOut as second parameter and then text will be aligned on right side of canvas.