Blackberry支持下标吗?我找到了BlackBerry论坛帖子"Subscript and superscript in RichTextField",但我无法访问BlackBerry知识库文章。 我怎样才能实现上标& LabelField中的下标?
答案 0 :(得分:1)
如果访问BlackBerry知识库(即来自某些国家/地区?)时出现问题,则该页面的内容(由@MSohm of RIM发布):
RichTextField不能本地支持下标,上标或 多种颜色。多种字体,字体大小和字体格式 (例如,Bold,Italic,Underlined)受支持。下列 链接进一步解释了这一点。
操作方法 - 格式化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);
}
给出: