2013-02-09 6 views
2

동일한 필드에서 다른 글꼴 크기로 페인트 방법으로 텍스트를 가져올 수있는 방법은 무엇입니까? 당신이 정말로 오버라이드 (override) paint() 방법 내부의 글꼴 크기를 변경하여이 작업을 수행하려면같은 필드의 다른 글꼴 크기

답변

4

, 당신은 다음과 같이 사용할 수 있습니다, I 필드 NON_FOCUSABLE했습니다

public TextScreen() { 
     super(MainScreen.VERTICAL_SCROLL | MainScreen.VERTICAL_SCROLLBAR); 

     final int MAX_FONT_SIZE = 24; 
     // this is the font style to use, but the SIZE will only apply to the 
     // beginning of the text 
     Font f = Font.getDefault().derive(MAX_FONT_SIZE); 

     TextField text = new TextField(Field.NON_FOCUSABLE) { 
     public void paint(Graphics g) { 
      char[] content = getText().toCharArray(); 

      Font oldFont = g.getFont(); 
      int size = MAX_FONT_SIZE; 
      // use the baseline for the largest font size as the baseline 
      // for all text that we draw 
      int y = oldFont.getBaseline(); 
      int x = 0; 
      int i = 0; 
      while (i < content.length) { 
       // draw chunks of up to 3 chars at a time 
       int length = Math.min(3, content.length - i); 
       Font font = oldFont.derive(Font.PLAIN, size--); 
       g.setFont(font); 

       g.drawText(content, i, length, x, y, DrawStyle.BASELINE, -1); 

       // skip forward by the width of this text chunk, and increase char index 
       x += font.getAdvance(content, i, length); 
       i += length; 
      } 
      // reset the graphics object to where it was 
      g.setFont(oldFont); 
     }      
     }; 

     text.setFont(f); 
     text.setText("Hello, BlackBerry font test application!"); 

     add(text); 
    } 

참고 경우 때문에 이 같은 paint()에서 글꼴을 변경하여 필드를 속여, 파란색 커서는 기본 텍스트와 일치하지 않습니다. drawFocus()을 무시하고 아무것도 수행하지 않고 커서를 제거 할 수도 있습니다.

포커스 요구 사항을 지정하지 않았으므로 원하는 내용을 알지 못했습니다.

다른 대안을 고려하고 싶다면 RichTextField은 동일한 필드 내에서 글꼴 크기 (또는 다른 텍스트 속성)를 변경하는 데 더 적합하다고 생각합니다. 내 예제처럼 텍스트를 점차 축소하려는 경우이 paint() 구현은 괜찮을 것입니다. 귀하의 분야에서 특정 단어을 선택하여 (예 : <span> 태그를 사용하는 것과 같이) 더 큰 글꼴로 그리려면 RichTextField이 아마도 가장 좋은 방법 일 것입니다.

enter image description here

: 여기

내 예제 코드의 출력입니다