2011-09-26 71 views
3

我试图加载自定义字体,它的工作原理。现在我遇到了问题,Iam无法在应用程序中使用多个自定义字体。如果我添加三个不同字体的labelField,则只有最后一个自定义字体用于所有标签。加载多个自定义字体黑莓

如果我删除最后一个标签的setFont,则使用第二个字体。有没有使用最后一种字体的机制?

这里是一个添加LabelFields代码,并设置字体

 LabelField TestLabel1 = new LabelField("Test Label 1"); 
    Font fo1 = loadCustomFonts("FirstFont.ttf", "FirstFont", 0, 30); 

    TestLabel1.setFont(fo1); 
    add(TestLabel1); 

    LabelField TestLabel2 = new LabelField("Test Label 2"); 
    Font fo2 = loadCustomFonts("SecondFont.ttf", "SecondFont", 0, 30); 
    TestLabel2.setFont(fo2); 
    add(TestLabel2); 

    LabelField TestLabel3 = new LabelField("Test Label 3"); 
    Font fo3 =loadCustomFonts("ThirdFont.TTF", "ThirdFont", 0, 30); 
    TestLabel3.setFont(fo3); 
    add(TestLabel3); 

这里是我的“loadCustomFonts”方法,它加载自定义字体,并返回一个字体对象。

public Font loadCustomFonts (String path, String fontname,int fontStyle, int fontSize){ 
InputStream stream = this.getClass().getResourceAsStream(path); 
    if (FontManager.getInstance().load(stream, fontname, FontManager.APPLICATION_FONT) == FontManager.SUCCESS){ 
     try{ 
      FontFamily family; 
      add(new LabelField("A")); 
      family = FontFamily.forName(fontname); 
      Font myFont = family.getFont(fontStyle,fontSize); 
      return myFont; 
     } 
     catch (ClassNotFoundException e){System.out.println(e.getMessage());} 
    } 
    else { 
     try{ 
      FontFamily family; 
      add(new LabelField("B")); 
      family = FontFamily.forName(fontname); 
      Font myFont = family.getFont(fontStyle,fontSize); 
      return myFont; 
     } 
     catch (ClassNotFoundException e){System.out.println(e.getMessage());} 
    } 
    return null; 
} 

回答

1

尝试覆盖每个labelfield的paint()并在其中应用setFont。

+0

我试着用下面的覆盖,但它不工作 “的LabelField testLabel2 =新的LabelField(“测试标签2 “){ \t公共无效涂料(图形图像){ \t \t \t字体FO2 = loadCustomFonts(” SmartCourierEUR-ExtraBoldCn.ttf”, “SmartCourierEUR-ExtraBoldCn”,0,30); \t \t \t System.out.println(“font 2:”+ fo2); \t \t \t this.setFont(fo2); \t} }; add(testLabel2);' – Martin

+0

add super.paint(graphics);在setfont –

0
 LabelField lf1 = new LabelField("This is font 1"){ 

     protected void paint(Graphics graphics) 
     { 
      setFont(getFont().derive(Font.STYLE_BOLD,15)); 
      graphics.drawText(getText(), 1, 1); 
      super.paint(graphics); 

     } 
    }; 

    LabelField lf2 = new LabelField("This is font 2"){ 

     protected void paint(Graphics graphics) 
     { 
      setFont(getFont().derive(Font.STYLE_ITALIC,18)); 
      graphics.drawText(getText(), 1, 1); 
      super.paint(graphics); 

     } 
    }; 
    LabelField lf3 = new LabelField("This is font 3"){ 

     protected void paint(Graphics graphics) 
     { 
      setFont(getFont().derive(Font.STYLE_UNDERLINED,20)); 
      graphics.drawText(getText(), 1, 1); 
      super.paint(graphics); 

     } 
    }; 

    add(lf1); 
    add(lf2); 
    add(lf3); 
+0

之后,你能解释一下我可以在我的代码中加载自定义字体的位置吗? – Martin

+0

@Martin你可以像这样添加你的自定义字体:setFont(loadCustomFonts(“SecondFont.ttf”,“SecondFont”,0,30)); –

4

我找到了原因。这是每个自定义字体的最大大小为60kb,并且如果该字体已经加载完毕。

所以我改变了加载器的方法。

public Font loadCustomFonts (String path, String fontname,int fontStyle, int fontSize){ 
    //Loads custom Fonts   

     InputStream stream = this.getClass().getResourceAsStream(path); 
     System.out.println("fontname"+fontname); 

     //System.out.println("fontname"+fontname +"FontManager Return "+FontManager.getInstance().load(path, fontname, FontManager.EXCEEDS_LIMIT)); 
     if (FontManager.getInstance().load(stream, fontname, FontManager.APPLICATION_FONT) == FontManager.EXCEEDS_LIMIT) 
      System.out.println("FontManager.EXCEEDS_LIMIT => true"); 
     else 
      System.out.println("FontManager.EXCEEDS_LIMIT => false");    

     System.out.println (FontManager.DUPLICATE_DATA); 

     if (FontManager.getInstance().load(path, fontname, FontManager.APPLICATION_FONT) == FontManager.APPLICATION_FONT) 
     { 
     //if (FontManager.getInstance().load(stream, fontname, FontManager.APPLICATION_FONT) == FontManager.SUCCESS){ 
      System.out.println("A getInstanc => true"); 
      try{ 
       System.out.println("A Try"); 
       FontFamily family;  
       family = FontFamily.forName(fontname); 
       Font myFont = family.getFont(fontStyle,fontSize); 
       System.out.println("A return myFont"+myFont); 
       return myFont; 
      } 
      catch (ClassNotFoundException e){System.out.println(e.getMessage());} 
       System.out.println("A Catch"); 
     } 
     else { 
     //font could not be loaded 
      System.out.println("B getInstanc => false"); 
      System.out.println("FontManager.getInstance().load(path, fontname, FontManager.APPLICATION_FONT"+FontManager.getInstance().load(path, fontname, FontManager.APPLICATION_FONT)); 
      if (FontManager.getInstance().load(path, fontname, FontManager.APPLICATION_FONT) == FontManager.DUPLICATE_NAME) 
      //check if font already is loaded 
       try{ 
         System.out.println("B Try"); 
         FontFamily family;  
         family = FontFamily.forName(fontname); 
         Font myFont = family.getFont(fontStyle,fontSize); 
         System.out.println("B return myFont"+myFont); 
         return myFont; 
        } 
        catch (ClassNotFoundException e){System.out.println(e.getMessage());}    
      else 
       System.out.println("FontManager.getInstance().load(path, fontname, FontManager.APPLICATION_FONT"+FontManager.getInstance().load(path, fontname, FontManager.APPLICATION_FONT)); 

     } 
     return null; 
    } 

有人知道加载大于60kb的字体的解决方案吗?我已经尝试使用输入流,但它不起作用。

(极限“FontManager.EXCEEDS_LIMIT如果字体数据超过60K的大小。” - http://www.blackberry.com/developers/docs/5.0.0api/net/rim/device/api/ui/FontManager.html#load%28java.io.InputStream,%20java.lang.String,%20int%29

+0

你最好把你的后续问题作为一个单独的问题。 –