Thursday, March 19, 2009

How To : Format text in a RichTextField

How To A RichTextField can be used to present selectable, formatted text to a user. Multiple fonts, font sizes, and font formatting (for example, Bold, Italic, Underlined) can be used in a single RichTextField.

 

import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.system.*;
public final class RichTextFieldSample extends UiApplication
{
    public static void main(String[] args)
    {
            RichTextFieldSample theApp = new RichTextFieldSample();
            theApp.enterEventDispatcher();
    }
    public RichTextFieldSample()
    {
        //Define the screen.
        MainScreen mainScreen;
        //Defint the RichTextField.
        RichTextField rtField;
        //An array of fonts to be passed to the RichTextField.
        Font fonts[] = new Font[3];
        //A list of offsets of character positions of the string contained
        //in the RichTextField where font appearance will change.
        //The first value in the offset array should be 0,
        //the starting character of the string and the last value should
        //be the last character position in the string.
        int[] offset = new int[4];
        //An array of indexes that align with the offset values and indicate
        //the index of the font array of the font to use starting at an offset.
        byte[] attribute = new byte[3];
        //Get three instances of the default font.
        //On plain, one bold and one bold and italic.
        fonts[0] = Font.getDefault();
        fonts[1] = Font.getDefault().derive(Font.BOLD);
        fonts[2] = Font.getDefault().derive(Font.BOLD | Font.ITALIC);
        //The string of text we will display in the RichTextField.
        String richText = "This is how you create text with formatting!!!";
        //Offset will start a character 0 of the string.
        offset[0] = 0;
        //At character position 0 we want to use the font at
        // position 2 of the font array (bold and italic).
        attribute[0] = 2;
        //Change to the default (non-bold, non-italic) font at
        //the 4th character position.
        offset[1] = 4;
        attribute[1] = 0;
        //Change to bold font at the 33rd character position.
        offset[2] = 33;
        attribute[2] = 1;
        //Add the last character position to the offset.
        offset[3] = richText.length();
        //Instantiate mainScreen.
        mainScreen = new MainScreen();
        //Set the title of mainscreen.
        mainScreen.setTitle(new LabelField
          ("RichTextFieldSample Sample",
            LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH));
        //Instantiate rtField.
        rtField = new RichTextField
          (richText, offset, attribute, fonts,
           RichTextField.USE_TEXT_WIDTH);
        //Add the RichTextField to the MainScreen.
        mainScreen.add(rtField);
        //Push the MainScreen onto the stack.
        pushScreen(mainScreen);
    }
}

6 comments:

  1. Thanks for posting this example. Much better to follow than the user doc.

    ReplyDelete
  2. yep, thanks a bunch. i finally got text formatting :)

    ReplyDelete
  3. Thanks a lot R.K.
    It really helps lot.

    ReplyDelete
  4. I am not a code expert. I am dazed to see people saying thank you. Can someone help me, I don't know how it works.
    I am desperate to be able to format(B,I,U) text (email, sms,etc.)On my BB device.

    ReplyDelete
  5. Hi IAT, Just give a try the above code, you definitely can achieve what you want. Sorry for late response.

    ReplyDelete
  6. Hi Rakesh,

    Thanks for your response. I know that for me to use the code above it'll have to be copied and pasted somewhere. Where exactly would I paste the code on my BB device?

    ReplyDelete

Place your comments here...