[LIB] CenterText

Discussion specific to projects ideas and support.
Post Reply
bungeshea
Posts: 207
Joined: 14 Aug 2011, 08:45
Location: Australia
Contact:

[LIB] CenterText

Post 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
Last edited by bungeshea on 01 Aug 2012, 03:34, edited 1 time in total.
spillerrec
Posts: 358
Joined: 01 Oct 2010, 06:37
Location: Denmark
Contact:

Re: [LIB] CenterText

Post 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!" );
My blog: http://spillerrec.dk/category/lego/
RICcreator, an alternative to nxtRICeditV2: http://riccreator.sourceforge.net/
bungeshea
Posts: 207
Joined: 14 Aug 2011, 08:45
Location: Australia
Contact:

Re: [LIB] CenterText

Post 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.)
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: [LIB] CenterText

Post 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 ...
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest