Is there any way to do so?
I'm trying to right-align a constantly increasing float value that may range from 3 to 5 digits.
Thanks for the help!

EDIT: I'm using a 2.8" TFT LCD display (http://www.adafruit.com/products/1770) and not a standard 16-character LCD display. My apologies for forgetting to include this initially.
Last edited by andrewrc on Thu Sep 18, 2014 12:39 am, edited 1 time in total.
andrewrc

Posts: 2
Joined: Sun Nov 18, 2012 6:53 pm

Re: Right-align text in Adafruit_GFX library?

Hello andrewrc, and welcome to the Adafruit forums!

As a learning arduino programmer myself, I'd test the value and add spaces accordingly, if it were me. I am not familiar enough with the Adafruit GFX library to know if it has this feature built in, but I'd highly doubt it.

If you are using this for an LCD, you can always determine the spaces required before the given value, and then add them. This would always max the line at 16chars, which would keep your text right-aligned.

You may get better replies, but in the mean time to play with things, this is some sample code I whipped up, assuming you're using an LCD that can handle 16 chars on each line. Keep in mind this code is only a piece of code. It returns a string, so use it like: lcd.write ( floatAlignRight ( yourFloatVar ) ); or something.

Code: Select all | TOGGLE FULL SIZE
`String floatAlignRigiht ( float num ){  int    space  = 0;  String spaces = "";    //how large is this number?  //always assumes value has 2 digits after the decimal point.  if ( num < 10.0 ) space = 12; //10.0=4chars. 12+4=16, etc  else if ( num > 9.0 && 100.0 > num ) space = 11;  else if ( num > 999.00 ) space = 10;    //add the correct amount of spaces infront of our value  for ( uint8_t s=0; s<space; s++ ) spaces += F(" ");    //return value (or your code here)  return spaces + String ( num );}`

SeiRruf

Posts: 82
Joined: Thu Sep 04, 2014 3:07 am

Re: Right-align text in Adafruit_GFX library?

I apologize for not mentioning that I am using a graphical TFT screen and not a standard character LED display (tiredness is getting to me, apparently). I'll edit that into the first post as well. Maybe I can still utilize the principle of controlled spacing, however - thanks for the idea.
andrewrc

Posts: 2
Joined: Sun Nov 18, 2012 6:53 pm

Re: Right-align text in Adafruit_GFX library?

Just saw this post.

Would there be a way of centering a bit of text but allowing the y axis to be changed?

Let's say I wanted to center a string on the bottom of the LCD/OLED?

gotnull

Posts: 32
Joined: Sun Oct 21, 2012 6:43 pm

Re: Right-align text in Adafruit_GFX library?

If you're using a graphical display, you can use .setCursor() to place the text anywhere you want on the screen.

Posts: 58409
Joined: Thu Feb 11, 2010 2:51 pm

Re: Right-align text in Adafruit_GFX library?

adafruit_support_mike wrote:If you're using a graphical display, you can use .setCursor() to place the text anywhere you want on the screen.

I'm aware of the functions available. The problem is if the text is dynamically created then the setCursor() values would have to change as well so I'm after a function that centers any text on the screen.

gotnull

Posts: 32
Joined: Sun Oct 21, 2012 6:43 pm

Re: Right-align text in Adafruit_GFX library?

The GFX library doesn't support that. You'll have to calculate the line width and move the cursor position in your own code.

Posts: 58409
Joined: Thu Feb 11, 2010 2:51 pm

Re: Right-align text in Adafruit_GFX library?

how to calculate the line width?

sblantipodi

Posts: 10
Joined: Mon Jan 21, 2019 6:37 am

Re: Right-align text in Adafruit_GFX library?

We use a 5x7 font by default, with 1 pixel of space between letters, so if 'c' is the number of characters in the string, the length will be 6c-1.

Posts: 58409
Joined: Thu Feb 11, 2010 2:51 pm

Re: Right-align text in Adafruit_GFX library?

adafruit_support_mike wrote:We use a 5x7 font by default, with 1 pixel of space between letters, so if 'c' is the number of characters in the string, the length will be 6c-1.

you know that it could be a bit tricky if the string exceeds the width of the display.
if text wrapping is on, how can I center a long string that is two rows long?

sblantipodi

Posts: 10
Joined: Mon Jan 21, 2019 6:37 am

Re: Right-align text in Adafruit_GFX library?

You'll have to write the code for that as well.

The GFX library provides graphics primitives like geometric shapes, bitmaps, and character glyphs. It doesn't do formatting and layout.

Posts: 58409
Joined: Thu Feb 11, 2010 2:51 pm

Re: Right-align text in Adafruit_GFX library?

adafruit_support_mike wrote:We use a 5x7 font by default, with 1 pixel of space between letters, so if 'c' is the number of characters in the string, the length will be 6c-1.

thank you for the support mike, what is the height/width of the font if I set the textSize to 2? and why there is no difference between textSize 1 and textSize 0?

sblantipodi

Posts: 10
Joined: Mon Jan 21, 2019 6:37 am

Re: Right-align text in Adafruit_GFX library?

The textSize variable draws an NxN pixel square for each point in the 5x7 bitmap, so just multiply those values by textSize.

textSize=0 is invald (there's no point in uisng 0x0 pixel characters), so the GFX library drops back to the default size of 1.

Posts: 58409
Joined: Thu Feb 11, 2010 2:51 pm

Re: Right-align text in Adafruit_GFX library?

adafruit_support_mike wrote:The textSize variable draws an NxN pixel square for each point in the 5x7 bitmap, so just multiply those values by textSize.

textSize=0 is invald (there's no point in uisng 0x0 pixel characters), so the GFX library drops back to the default size of 1.

Thank you so much for the answer mike

sblantipodi

Posts: 10
Joined: Mon Jan 21, 2019 6:37 am