RichTextEdit avec le texte multicolore?
-
02-10-2019 - |
Question
Comment puis-je créer une RichTextEdit
en utilisant l'API RIM 4.5 qui contient du texte avec plusieurs couleurs?
Par exemple, je veux créer un RichTextEdit
comme suit:
- Le texte "Bonjour tout le monde BB"
- "Bonjour" devrait être bleu
- "BB monde" doit être rouge
- "BB" devrait être
ITALIC
- "Bonjour" devrait être
BOLD
Le principal problème devient couleurs, pas gras et en italique.
J'ai essayé la fonction de remplaçant RichTextEdit.paint
, mais ces formats la couleur de la chaîne, pas seulement une sous-chaîne de celui-ci!
Voici le code que je mis en œuvre pour rendre le texte en gras et en italique et en remplaçant la peinture pour changer toute la couleur de la chaîne:
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);
}
}