RichTextEdit 带有彩色文本?
如何使用 RIM 4.5 API 创建包含多种颜色文本的 RichTextEdit
?
例如,我想创建一个RichTextEdit
,如下所示:
- 文本是“Hello BB world”
- “Hello”应该是蓝色< /strong>
- “BB world” 应该是红色
- “BB” 应该是
斜体
- “你好” 应该是
BOLD
主要问题是获取颜色,而不是粗体和斜体。
我尝试过重写 RichTextEdit.paint 函数,但这会格式化整个字符串的颜色,而不仅仅是它的子字符串!
这是我实现的代码,用于使文本变为粗体和斜体,并覆盖油漆以更改整个字符串颜色:
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);
}
}
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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不幸的是,这个问题没有简单的答案。根据 这篇文章,RichTextField 不支持多种颜色(尽管存在
getForegroundColors
方法)。可以扩展 RichTextField 以支持多种颜色,但由于需要大量工作,从头开始实现您自己的
Field
可能会更容易。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.