Page 1 of 1

[LIB] CenterText

Posted: 31 Jul 2012, 05:03
by bungeshea
I have created an easy way to centre text for displaying on the NXT screen. Returns the X position of the text passed in through the first parameter.

Use:

Code: Select all

#include "CenterText.Lib.nxc"
TextOut( CenterText("Hello World!"), LCD_LINE3, "Hello World!" );
Remember to use the same text that you are displaying in the CenterText function.

Or use:

Code: Select all

#include "CenterText.Lib.nxc"
CenterTextOut(  LCD_LINE3, "Hello World!" );
Code: (for those too lazy to download)

Code: Select all

/*
	CenterText.Lib.nxc
*/

int CenterText( const string text ) {
	int len = StrLen(text);
	len = (len * 5);
	len = (100 - len);
	len = (len / 2);
	len = (len - 5);
	return len;
}

char CenterTextOut( int y, string text, unsigned long options = DRAW_OPT_NORMAL ) {
	int len = CenterText(text);
	return TextOut( len, y, text, options );
}
Download:
CenterText.lib.nxc
CenterText.Lib.nxc
(354 Bytes) Downloaded 333 times

Re: [LIB] CenterText

Posted: 31 Jul 2012, 12:17
by spillerrec
Just a suggestion:

Code: Select all

int CenterTextOut( int y, string text , unsigned long options = DRAW_OPT_NORMAL ){
   int len = StrLen(text);
   len = (len * 5);
   len = (100 - len);
   len = (len / 2);
   len = (len - 5);
   TextOut( len, text, options );
}

Code: Select all

#include "CenterText.Lib.nxc"
CenterTextOut( LCD_LINE3, "Hello World!" );

Re: [LIB] CenterText

Posted: 01 Aug 2012, 03:36
by bungeshea
I did think of doing something like that. Instead of replacing the CenterText function, how about adding to it by creating a CenterTextOut function that does exactly what your function does but insted uses the CenterText function to calculate length. (See first post for updated code.)

Re: [LIB] CenterText

Posted: 03 Aug 2012, 09:17
by HaWe
I agree with your int centertext-x function.
I'm only curious why you are using "5" for calculating internally:
len = (len * 5);

IIRC, the char width is 5+1 (for white space) = 6 ...