黑莓是否支持下标上标?我找到了 BlackBerry 论坛主题“RichTextField 中的下标和上标”,但我无法访问 BlackBerry 知识库文章。如何在 LabelField 中实现上标和下标?
1 回答
如果在访问 BlackBerry 知识库时遇到问题(例如,来自某些国家/地区?),以下是该页面的内容(由 RIM 的@MSohm 发布):
RichTextField 本身不支持下标、上标或多种颜色。支持多种字体、字体大小和字体格式(例如,粗体、斜体、下划线)。以下链接进一步解释了这一点。
如何 - 在 RichTextField 中设置文本格式 文章编号:DB-00124
http://supportforums.blackberry.com/t5/Java-Development/Format-text-in-a-RichTextField/ta-p/445038如何 - 更改字段的文本颜色 文章编号:DB-00114
http://supportforums.blackberry.com/t5/Java-Development/Change-the-text-color-of-a-field/ta-p/ 442951
如果您仍然想这样做,您可以尝试子类化RichTextField
, 或LabelField
, 并覆盖该paint()
方法。在那里,您可以更改字体大小并移动文本的 y 坐标。这取决于您希望解决方案的通用性。也许您可以发布有关您的问题的更多信息?
但是,作为一个非常简单的硬编码示例,以下代码将创建一个LabelField
打印输出:“ CO 2 ”
private class SubscriptLabelField extends LabelField {
private int _subscriptTop = 0;
private int _subscriptFontSize = 0;
public SubscriptLabelField(Object text, long style) {
super(text, style);
setFont(getFont());
}
public void setFont(Font newFont) {
super.setFont(newFont);
// we use a subscript that's located at half the normal font's height,
// and is 2/3 as tall as the normal font
int h = newFont.getHeight();
_subscriptTop = h / 2;
_subscriptFontSize = 2 * h / 3;
super.invalidate();
}
protected void layout(int width, int height) {
super.layout(width, height);
// add more space at the bottom for the subscript
int w = getExtent().width;
int h = getExtent().height;
int extraHeight = _subscriptFontSize - (getFont().getHeight() - _subscriptTop);
setExtent(w, h + extraHeight);
}
public void paint(Graphics g) {
// here we hardcode this method to simply draw the last char
// as a "subscript"
String text = getText();
String normalText = text.substring(0, text.length() - 1);
g.drawText(normalText, 0, 0);
// how much space will the normal text take up, horizontally?
int advance = g.getFont().getAdvance(normalText);
// make the subscript a smaller font
Font oldFont = g.getFont();
Font subscript = getFont().derive(Font.PLAIN, _subscriptFontSize);
g.setFont(subscript);
String subscriptText = text.substring(text.length() - 1);
g.drawText(subscriptText, advance, _subscriptTop);
// reset changes to graphics object just to be safe
g.setFont(oldFont);
}
}
然后像这样使用它:
public SubscriptScreen() {
super(MainScreen.VERTICAL_SCROLL | MainScreen.VERTICAL_SCROLLBAR);
SubscriptLabelField textField = new SubscriptLabelField("C02", LabelField.NON_FOCUSABLE);
// TODO: this line is just to show the adjusted boundaries of the field -> remove!
textField.setBackground(BackgroundFactory.createSolidBackground(Color.LIGHTGRAY));
add(textField);
}
这使: