0

我正在使用 BlackBerry 知识中心教程“如何格式化 RichTextField ”为我的 RichTextField 设置格式。

我在格式化文本时遇到了一些困难,例如:

AI 走在街上 B 突然看到一只会飞的狗

如果我只想加粗字母 A 和 BI 需要有它们的字符串索引和长度。

我创建了 2 个数组,一个处理整个文本中字母的索引,第二个数组处理每个字母索引的长度,例如:A(长度 1),WC(长度 2)。

我试图循环运行它,但它不起作用:

Font fonts[] = new Font[2];
    int[] offset = new int[3];
    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);

    for (int i = 0; i<lettersLength; i++) {
      offset[0] = letterIndexes[i]; //handles the indexes of the letters in the entire text
      attribute[0] = 1;
      offset[1] = letterLength[i]; //handles each letter index
    }
4

1 回答 1

0
offsets[0] = 0; // index of A, first character
attribute[0] = 1;  // Choose the bold font starting at offsets[0]
offsets[1] = 1;  // 1 past the desired bold
attributes[1] = 0;  // assign the regular font starting at offset[1]
offsets[2] = index of 'B';
attributes[2] = 1; // set the style to the bold font for this
offsets[3] = index of 'B' + 1; // the new non bold segment starts just after the B
attributes[3] = 0; // set the style to normal

对不起,不是更好的代码,但希望它有所帮助。

于 2011-07-04T16:48:28.080 回答