2012-04-12 36 views
3

私のアプリにカスタムLabelFieldを実装しています。小さなフォントでうまくいきますが、フォントサイズを大きくすると正しく表示されません。ここであなたはこのイメージでそれを見ることができます。BlackberryのカスタムLabelFieldでテキストが正しく表示されない

enter image description here

あなたはそれがテキストの半分だけを示している画像で見ることができます。どのように私はこれを克服することができます。

ここでは、カスタムLabelFieldのコードを提供しています。私が間違っていることを教えてください。あなたは現在のデフォルトのフォントの高さにあなたのフィールドの高さを設定しているあなたのlayout方法で

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

} 

答えて

3

。つまり、大きなフォントを使用して描画すると、テキストが切り取られます。これはにあなたのレイアウト方法を変更する解決するために:

protected void layout(int width, int height) { 

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

これは、ピクセルにご希望のフォントサイズを変換して、フィールドの高さを設定することを使用しています。

3

CustomLabelFieldを作成するにはどうすればLabelFieldになりますか?コンストラクタで適切なスタイルビットを設定すると、レイアウト、テキストのペイントの複雑さ(アライメント、ラップ、スタイルの配慮など)およびその他のタスクが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); 
    } 
} 
関連する問題