开发者

Custom fonts in android

I am trying to use custom fonts in a textview:

tv=(TextView)findViewById(res);
Typeface font = Typeface.createFromAsset(this.getAssets(), "fonts/font.ttf"); 
tv.setTypeface(font); 

But when I run I get the following error:

W/System.err(  542): java.lang.开发者_如何学编程RuntimeException: native typeface cannot be made

Whats the issue?


For me, this definitely was the message I received when the font file couldn't be found. Something as simple as:

Typeface.createFromAsset(getContext().getAssets(), "fonts/MYFONT.TTF");

When my font was actually in font/MYFONT.TTF


First of all check font's name and extension. it is case sensitive & probably all caps. eg.

Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/ABADDON.TTF")


I would guess that there is a problem with the font itself. That error will be triggered when native code in the OS attempts to load the typeface. I seem to recall that there's a different message if the file is missing, so I think it is finding the file but not liking it for some reason.


I had encountered this problem i was setting the typeface inside a custom layout class with a constructor that pass a reference to parent activity's "context" and setting it up like this:

Typeface font = Typeface.createFromAsset(this.getAssets(), "fonts/font.ttf");

it gives me "native typeface cannot be made" error.

went on creating a new test project out from scratch to just display the "Hello World, " with the custom font i want to use so i did this on onCreate() on the default activity class:

Typeface font = Typeface.createFromAsset(getAssets(), "fonts/font.ttf");

and this time it worked and i thought that maybe i should try putting a reference of the main Activity rather than the Context to my custom layout class:

Typeface font = Typeface.createFromAsset(activity.getAssets(), "fonts/font.ttf");

now this time it worked on the custom layout class. hope this would help you guys too.


This might be the issue

Typeface.createFromAsset leaks asset stream : http://code.google.com/p/android/issues/detail?id=9904


public class Harshida extends View {

Bitmap gBall;
float changingY;
Typeface font;
public Harshida(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
    gBall=BitmapFactory.decodeResource(getResources(), R.drawable.greenball);
    changingY=0;
    font=Typeface.createFromAsset(context.getAssets(), "assets/G-Unit.TTF");
}
@Override
protected void onDraw(Canvas canvas) {
    // TODO Auto-generated method stub
    super.onDraw(canvas);
    canvas.drawColor(Color.WHITE);

    Paint textPaint=new Paint();
    textPaint.setARGB(50,254,10,50);
    textPaint.setTextAlign(Align.CENTER);
    textPaint.setTextSize(50);
    textPaint.setTypeface(font);
    canvas.drawText("HarshidaParmar",canvas.getWidth()/2,200,textPaint);
    //canvas.drawBitmap(gBall,(canvas.getWidth()/2),0,null);
    canvas.drawBitmap(gBall,(canvas.getWidth()/2),changingY,null);
    if(changingY < canvas.getHeight()){
        changingY +=10;
    }else {
        changingY=0;
    }
    Rect middleRect= new Rect();
    middleRect.set(0, 40, canvas.getWidth(),400);
    //middleRect.set(0,0,0,0);
    Paint ourBlue = new Paint();
    ourBlue.setColor(Color.BLUE);
    canvas.drawRect(middleRect, ourBlue);
    //canvas.drawRect(middleRect,ourBlue);
    invalidate();

}

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜