I am using the BlackBerry knowledge center tutorial "How To Format RichTextField" to set a format for my RichTextField.
I encountered some difficulty when formatting text like:
A I was walking in the street B suddenly I saw a flying dog
If I want to bold only the letters A & B I need to have their string indexes and length.
I created 2 arrays, one handles the indexes of the letters in the entire text and the second array handles the length of each letter index, for example: A (length 1), WC(length 2).
I tried to run it in a loop but it doesn't work:
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
}
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
Sorry not nicer code, but hope it helps.