我尝试加载自定义字体,这有效。现在我遇到了问题,我不能在应用程序中使用多个自定义字体。如果我添加三个带有不同字体的labelFields,则只有最后一个自定义字体用于所有标签。
如果我删除最后一个标签的最后一个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”方法,它加载自定义Font并返回一个字体对象。
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;
}
答案 0 :(得分: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)
答案 1 :(得分:1)
尝试覆盖每个labelfield的paint()并在其中应用setFont。
答案 2 :(得分: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);