【问题标题】:How to implement Superscript & Subscript in Blackberry如何在黑莓中实现上标和下标
【发布时间】:2013-05-25 11:06:40
【问题描述】:

黑莓是否支持下标上标?我找到了 BlackBerry 论坛线程 "Subscript and superscript in RichTextField",但我无法访问 BlackBerry 知识库文章。 如何在 LabelField 中实现上标和下标?

【问题讨论】:

    标签: blackberry java-me


    【解决方案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


    如果您仍想这样做,您可以尝试继承 RichTextFieldLabelField,并覆盖 paint() 方法。在那里,您可以更改字体大小并移动文本的 y 坐标。这取决于您希望解决方案的通用性。也许您可以发布有关您的问题的更多信息?

    但是,作为一个非常简单的硬编码示例,以下代码将创建一个LabelField,并打印出:“CO2 "

       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);
    }
    

    给出:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-26
      • 2010-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多