Download MetaTrader 5

Text justification

To add comments, please log in or register
Andrea Bronzini
108
Andrea Bronzini  

Hi All :)

writing a custom indicator I came across a problem... hot to place text in the right position.

As I create text object I can see that the text is justified "bottom-center" and I did not found any parameter to change it :(

I need my object text to be aligned "mid-left"... or otherwise the handle of the textbox shall be in at middle-left instead top-center as it is by deafult.


Is there any way to set this???

Thanks for answers.



Cheers,

Zyp

Blooper1980
99
Blooper1980  
Zypkin:

Hi All :)

writing a custom indicator I came across a problem... hot to place text in the right position.

As I create text object I can see that the text is justified "bottom-center" and I did not found any parameter to change it :(

I need my object text to be aligned "mid-left"... or otherwise the handle of the textbox shall be in at middle-left instead top-center as it is by deafult.


Is there any way to set this???

Thanks for answers.



Cheers,

Zyp

Hello there. I found this function somewhere. It's not mine, but work pretty good.

Hope this helps a bit.



void CreateTextLable
(string TextLableName, string Text, int TextSize, string FontName, color TextColor, int TextCorner, int X, int Y)
{
//---
ObjectCreate(TextLableName, OBJ_LABEL, 0, 0, 0);
ObjectSet(TextLableName, OBJPROP_CORNER, TextCorner);
ObjectSet(TextLableName, OBJPROP_XDISTANCE, X);
ObjectSet(TextLableName, OBJPROP_YDISTANCE, Y);
ObjectSetText(TextLableName,Text,TextSize,FontName,TextColor);
}


CreateTextLable("label_object8",nametext8,20,"Times New Roman",COL8,0,30,160);


COL8 = COLOUR eg. Yellow etc. Textname8 is where the text will go.

Andrea Bronzini
108
Andrea Bronzini  
Blooper1980:

Hello there. I found this function somewhere. It's not mine, but work pretty good.

Hope this helps a bit.



Hi Blooper,

unfortunately doesnt help... this function shows how to set properties of a Label object, not Text.

The difference is that Label are fixed in position as defined by X,Y values while Text is positioned on the chart and moves with the time.


Tnx anyway :)



Zyp

Blooper1980
99
Blooper1980  
Zypkin:

Hi Blooper,

unfortunately doesnt help... this function shows how to set properties of a Label object, not Text.

The difference is that Label are fixed in position as defined by X,Y values while Text is positioned on the chart and moves with the time.


Tnx anyway :)



Zyp

I see what U mean.. If U find out, please post it. I also would like to know. :)

pepito perez
73
pepito perez  
Blooper1980:

I see what U mean.. If U find out, please post it. I also would like to know. :)


left justified:


int TextLen=StringLen(text);
for(int i=1;i<TextLen;i++)text=" " + text;
ObjectSetText(ObjName, text, 8, "Lucida Sans Typewriter", Black);



Rafael León
13
Rafael León  


I have written this very simple function to align numbers to the right:

//-------------------------------------------------------------------------+
// Function to convert text to string and right align (© Rafael León)      |
//-------------------------------------------------------------------------+
// PARAMETERS     ---------------------------------------------------------|
// varToAlign     : double var name to text convert and right align        |
// numChar        : leng of string "textOut"                               |
// numDec         : number of decimals                                     |
// EXAMPLE        ---------------------------------------------------------|
// double price   = 1.45;                                                  |
// ObjectCreate   ("Price",OBJ_LABEL,0,0,0,0,0);                           |
// ObjectSet      ("Price",OBJPROP_CORNER,0);                              |
// ObjectSet      ("Price",OBJPROP_XDISTANCE,100);                         |
// ObjectSet      ("Price",OBJPROP_YDISTANCE,100);                         |
// ObjectSetText  ("Price",rightAlign(price,6,2),7,"Consolas",White);      |
// -----------------"123456"-----------------------------------------------|
// Function returns "  1.45" inside "textOut" string.                      |
// Point "." is one more character.                                        |
// Use with "Consolas" or any other fixed-width font.                      |
//-------------------------------------------------------------------------+


string rightAlign (double varToAlign, int numChar, int numDec)     
  {
   string textOut=DoubleToStr(varToAlign,numDec);
   string blanks="";
   for(int f=0;f<=numChar-StringLen(textOut);f++) blanks=" "+blanks;
   textOut=blanks+textOut;         
   return (textOut);
  }

nicholishen
971
nicholishen  

If you test this make to to save it as "SetTextCorner.mq4"

//+------------------------------------------------------------------+
//|                                                SetTextCorner.mq4 |
//|                                      Copyright 2017, nicholishen |
//|                         https://www.forexfactory.com/nicholishen |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, nicholishen"
#property link      "https://www.forexfactory.com/nicholishen"
#property version   "1.00"
#property strict
#property indicator_separate_window

input ENUM_BASE_CORNER Corner = CORNER_RIGHT_UPPER;

#include <ChartObjects\ChartObjectsTxtControls.mqh>
CChartObjectLabel label;
int OnInit()
{
//--- indicator buffers mapping
  SetLabel();
//---
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

void SetLabel()
{
   int subwin = WindowFind("SetTextCorner");
   label.Create(0,"__label__",subwin,0,0); 
   switch(Corner)
   {
      case CORNER_LEFT_UPPER:
         label.Anchor(ANCHOR_LEFT_UPPER);
         label.Corner(CORNER_LEFT_UPPER);
         break;
      case CORNER_RIGHT_UPPER:
         label.Anchor(ANCHOR_RIGHT_UPPER);
         label.Corner(CORNER_RIGHT_UPPER);
         break;
      case CORNER_LEFT_LOWER:
         label.Anchor(ANCHOR_LEFT_LOWER);
         label.Corner(CORNER_LEFT_LOWER);
         break;
      case CORNER_RIGHT_LOWER:
         label.Anchor(ANCHOR_RIGHT_LOWER);
         label.Corner(CORNER_RIGHT_LOWER);
         break;
   }
   label.Description("This is a test label.");
   label.Font("Consolas");
   label.FontSize(8);
   label.Color(clrWhite);
}
To add comments, please log in or register