質問

I am implementing Custom LabelField in my app. It is working fine with small fonts, when I increase the font size it will not show properly. Here you can see it in this image.

enter image description here

You can see in the image it is showing only the half of the text. How can I overcome this.

Here I am giving my code for custom LabelField. Please tell me what I am doing wrong.

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

}
役に立ちましたか?

解決

In your layout method you are setting the height of your field to the current default font height. This means that when you draw using a larger font the text is cropped. To solve this change your layout method to:

protected void layout(int width, int height) {  

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

This converts your desired font size into pixels and uses that to set your field height.

他のヒント

What about creating your CustomLabelField by just extending LabelField? Then the layout, the complexity of painting text (alignment, wrap, style consideration etc.) and other tasks will be done by the LabelField itself if you set the appropriate style bit in constructor.

And you can just apply any font on that Field by just invoking setFont(Font). The field itself will adjust its size and drawing. Check following snippets.


CustomLabelField implementation:

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


Example:

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);
    }
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top