黑莓中自定义标签字段出现文字不清晰的问题

3
我正在实现自定义的标签字段(LabelField)在我的应用程序中。当使用小字体时它可以正常工作,但是当我增大字体大小时会出现显示不正常的情况。您可以在此图片中看到。

enter image description here

从图片中可以看到,它只显示文本的一半。我该如何解决这个问题。

这里是我自定义LabelField的代码。请告诉我我做错了什么。

public class CustomLabelField extends Field  
{  
    private String label;  
    private int fontSize;
    private int foregroundColor;  
    private int backgroundColor;  
    public CustomLabelField(String label, int fontSize, int foregroundColor,  
                     int backgroundColor,long style)  
    {  
        super(style);  
        this.label = label;  
        this.foregroundColor = foregroundColor;  
        this.backgroundColor = backgroundColor;  
        this.fontSize = fontSize;
    }  

   protected void layout(int width, int height) {  

       setExtent(width, getFont().getHeight());  


   }  

    protected void paint(Graphics graphics) {  

       graphics.setBackgroundColor(backgroundColor);  
       graphics.clear();  
       graphics.setFont(setMyFont());  
       graphics.setColor(foregroundColor);  
       graphics.drawText(label, 0, 0, (int)(getStyle()& DrawStyle.ELLIPSIS | DrawStyle.HALIGN_MASK),getWidth() - 6);  
   }  

   // Get font for the label field  
   public Font setMyFont()  
   {  
     try {  
         FontFamily alphaSansFamily = FontFamily.forName("BBAlpha Serif");  
         Font appFont = alphaSansFamily.getFont(Font.PLAIN, fontSize, Ui.UNITS_pt);  
         return appFont;  

    } catch (ClassNotFoundException e) {  
        // TODO Auto-generated catch block  
        e.printStackTrace();  
    }  
    return null;  
  }  

}
2个回答

3
你可以通过扩展LabelField来创建自定义标签字段CustomLabelField。如果在构造函数中设置适当的样式位,那么布局、绘制文本的复杂度(对齐、换行、样式考虑等)以及其他任务都将由LabelField自己完成。
你只需调用setFont(Font)就可以在该Field上应用任何字体。该字段本身会调整其大小和绘制。请查看以下代码片段。
CustomLabelField实现:
class CustomLabelField extends LabelField {
    private int foregroundColor;
    private int backgroundColor;

    public CustomLabelField(String label, int foregroundColor,
            int backgroundColor, long style) {
        super(label, style);
        this.foregroundColor = foregroundColor;
        this.backgroundColor = backgroundColor;
    }

    protected void paint(Graphics graphics) {
        graphics.setBackgroundColor(backgroundColor);
        graphics.clear();
        graphics.setColor(foregroundColor);
        super.paint(graphics);
    }
}


示例:

class MyScreen extends MainScreen {

    public Font getMyFont(int fontSize) {
        try {
            FontFamily alphaSansFamily = FontFamily.forName("BBAlpha Serif");
            return alphaSansFamily.getFont(Font.PLAIN, fontSize, Ui.UNITS_pt);
        } catch (Exception e) {
        }
        return null;
    }

    public MyScreen() {
        CustomLabelField clf = new CustomLabelField(
                    "Main",
                    Color.WHITE,
                    Color.BLACK,
                    LabelField.ELLIPSIS);

        // Font setup
        clf.setFont(getMyFont(20));

        add(clf);
    }
}

3

在你的layout方法中,你正在将字段的高度设置为当前默认字体的高度。这意味着当你使用更大的字体绘制文本时,文本会被裁剪。要解决这个问题,请将你的layout方法更改为:

protected void layout(int width, int height) {  

    int fieldHeight = Ui.convertSize(fontSize, Ui.UNITS_pt, Ui.UNITS_px);
    setExtent(width, fieldHeight);  
}  

这会将您所需的字体大小转换为像素,并使用该大小来设置字段高度。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接