开发者

RichTextEdit with multicolored text?

How do I create a RichTextEdit using RIM 4.5 API that contains text with multiple colors?

For example I want to create a RichTextEdit as follows:

  • The Text is "Hello BB world"
  • "Hello" should be blue
  • "BB world" should be red
  • "BB" should be ITALIC
  • "Hello" should be BOLD

The main problem is getting colors, not the bold and italic.

I have tried overriding RichTextEdit.paint function, but this formats the color of the whole string, not just a substring of it!

Here's the code I implemented to make the text bold and italic and overriding the paint to change the whole string color:

public final class RichTextFieldSample extends UiApplication 
{ 
    public static void main(String[] args) 
    { 
  开发者_如何学编程      RichTextFieldSample theApp = new RichTextFieldSample(); 
        theApp.enterEventDispatcher(); 
    } 
    public RichTextFieldSample() 
    { 
        String richText = "This is how you create text with formatting!!!";
        Font fonts[] = new Font[3]; 
        int[] offset = new int[4]; 
        byte[] attribute = new byte[3]; 
        fonts[0] = Font.getDefault(); 
        fonts[1] = Font.getDefault().derive(Font.BOLD); 
        fonts[2] = Font.getDefault().derive(Font.BOLD | Font.ITALIC); 
        offset[0] = 0; 
        attribute[0] = 2; 
        offset[1] = 4; 
        attribute[1] = 0; 
        offset[2] = 33; 
        attribute[2] = 1; 
        offset[3] = richText.length(); 

        RichTextField rtField = new RichTextField 
          (richText, offset, attribute, fonts, 
           RichTextField.USE_TEXT_WIDTH) {
                protected void paint(Graphics graphics) {
                    graphics.clear();
                    graphics.setColor(0x0000FF);
                    super.paint(graphics);
                }
        };

        MainScreen mainScreen = new MainScreen(); 
        mainScreen.setTitle(new LabelField 
          ("RichTextFieldSample Sample", 
            LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH)); 
        mainScreen.add(rtField); 
        pushScreen(mainScreen); 
    } 
}


Unfortunately no easy answer to this one. According to this post, RichTextField doesn't support multiple colors (despite the presence of a getForegroundColors method).

It may be possible to extend RichTextField to support multiple colors, but with the amount of work necessary, it would very likely be easier to implement your own Field from scratch.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜