Search code examples
blackberrycustom-fieldslabelfield

Text is not coming properly with Custom LabelField in Blackberry


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

}

Solution

  • 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.