RichTextEdit con el texto multicolor?
-
02-10-2019 - |
Pregunta
¿Cómo se crea un RichTextEdit
utilizando RIM 4.5 API que contiene texto con varios colores?
Por ejemplo, yo quiero crear un RichTextEdit
de la siguiente manera:
- El texto es "Hola mundo BB"
- "Hola" debería ser azul
- "mundo BB" debería ser Red
- "BB" debería ser
ITALIC
- "Hola" debería ser
BOLD
El problema principal es conseguir colores, no la negrita y cursiva.
He intentado anular la función RichTextEdit.paint
, pero esto formatos del color de toda la cadena, no sólo una subcadena de ella!
Este es el código que he implementado para hacer que el texto en negrita y cursiva y anulando la pintura para cambiar todo el color de cadena:
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);
}
}
Solución
Por desgracia hay respuesta fácil a esta. Según este post , RichTextField no admite varios colores (a pesar de la presencia de un método getForegroundColors
).
Puede ser posible extender RichTextField para soportar múltiples colores, pero con la cantidad de trabajo necesario, sería muy probable que sea más fácil de poner en práctica su propia Field
desde cero.