Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 90
Registered: ‎09-07-2010
My Device: Not Specified

Different font and color for same LabelField/RIchTextField etc.

Hello, I know this should be very easy, but at this point I just can't get it. How can I set different color or font in the same piece of text (LabelField, RichTextField, etc) just like Facebook app?

 

Facebook.jpg

 

See? "New England Patriots" is different color and font than the other text, but the other text is not indented. Right now I'm setting different LabelField for each different color/font, and I have it like this though:

 

Dibujo.PNG

 

And, for example, "Pony Pony Run..." exceeds  the size, but it gets indented after "es fan de" though. How can I have it like Facebook (and many other) apps?

 

Thanks in advance,

 

 

 

Developer
Posts: 2,268
Registered: ‎07-08-2009
My Device: various
My Carrier: various

Re: Different font and color for same LabelField/RIchTextField etc.

LabelField is too simplistic for that; start looking at RichTextField and (for colors) ActiveRichTextField.

 

Ironically, RichTextField does carry the color information about its regions (see RichTextField.getBackgroundColors and getForegroundColors), but there is no documented way to set them. I would try using RichTextField.setAttributes(int[] colorForeground, int[] colorBackground), as ActiveRichTextField.setAttributes claims to be overriding the above.

----------------------------------------------------------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!
Highlighted
Developer
Posts: 2,268
Registered: ‎07-08-2009
My Device: various
My Carrier: various

Re: Different font and color for same LabelField/RIchTextField etc.

An update (after debugging):

 

1) RichTextField.setAttributes method exists (undocumented but working) only in 5.0 and above.

 

2) The array sizes on colorForeground and colorBackground have to correspond to the size of the previously used fonts array (see RichTextField(String text, int[] offsets, byte[] attributes, Font[] fonts, long style)). attributes given in that constructor or a subsequent setText(String text, int[] offsets, byte[] attributes, Font[] fonts) are indexing both fonts array and the pair of color arrays.

 

Here is the code example and the screenshot (attached):

 

		Font[] fonts = new Font[3];
		fonts[0] = Font.getDefault().derive(Font.PLAIN, 50);
		fonts[1] = fonts[0].derive(Font.BOLD);
		fonts[2] = fonts[0];
		byte[] attribs = new byte[] {0, 1, 0};
		int[] offsets = new int[] {0, 4, 11, 16};
		int[] backColors = new int[] {Color.LIGHTGREEN, Color.YELLOW, Color.WHITE};
		int[] foreColors = new int[] {Color.BLACK, Color.BLUE, Color.RED};
		RichTextField testText = new RichTextField("The colored text", offsets, attribs, fonts, Field.NON_FOCUSABLE);
		try {
			testText.setAttributes(foreColors, backColors);
		} catch (Exception e) {
			System.out.println("### Caught: " + e.toString() + " exception on RichTextField.setAttributes!!!!");
		}

 

ColoredRichTextFieldScreen.png

Note that the third pair of colors (white background / red font) was not used - thanks to that discriminating attributes array.

 

If you cannot see the image, just click on attachment.

----------------------------------------------------------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!