2012-04-12 4 views
3

내 앱에서 맞춤 LabelField를 구현 중입니다. 그것은 제대로 표시되지 않습니다 글꼴 크기를 늘리면 작은 글꼴로 잘 작동합니다. 여기에서이 이미지에서 볼 수 있습니다.Blackberry의 Custom 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

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