Text justification

 

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

 
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.

 
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

 
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. :)

 
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);



 


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);
  }

 

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);
}
 
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);
  }


The function is great! Many thanks!

 
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);
  }

almost 3 years later, thank you very much, it's exactly what I was looking for! :)

 

Same thing, but a bit different coding..

//+------------------------------------------------------------------+
//|    String Alignment                                              |
//+------------------------------------------------------------------+
string StringLeftPad(string str, int n=1, string str2=" ")
{
        return(StringRepeat(str2,n-StringLen(str)) + str);
}

string StringRepeat(string str, int n=1)
{
        string outstr = "";
        for(int i=0; i<n; i++)
        {
                outstr = outstr + str;
        }
        return(outstr);
}
//+------------------------------------------------------------------+
 
Andrea Bronzini: I need my object text to be aligned "mid-left"... 
Blooper1980: I see what U mean.. If U find out, please post it. I also would like to know. :)
  1. Perhaps you should read the manual.
       How To Ask Questions The Smart Way. 2004
          How To Interpret Answers.
             RTFM and STFW: How To Tell You've Seriously Screwed Up.

  2. What part of "Anchor point position relative to the text can be selected from ENUM_ANCHOR_POINT enumeration," and "InpAnchor" in the examples, was unclear?
             OBJ_TEXT - Object Types - Objects Constants - Constants, Enumerations and Structures - MQL4 Reference

Reason: