Drawing text on canvas with WINGDING.ttf font is not working on android

偶尔善良 提交于 2020-01-03 00:27:51

问题


When I am drawing Text with custom font(basically icon ex: WINGDING.ttf) using drawText is showing simple text as provided.

Steps I followed: 1. Added font file to Assets folder 2. Set paint with the added font 3. drawing text with the paint

To draw text I used the corresponding English character

canvas.drawText("p",0,1, x, y, myPaint);

This displays as p on app


回答1:


use this way if you have assets/fonts:

    private Paint    myPaint= new Paint(Paint.ANTI_ALIAS_FLAG);  
    private Typeface mFace; 
    mFace = Typeface.createFromAsset(getContext().getAssets(),"fonts/WINGDING.ttf");  
    myPaint.setTextSize(32);
    myPaint.setTypeface(mFace);
    canvas.drawText("test test",0,1, x, y, myPaint);
  //canvas.drawText("test test", 10, 200, myPaint);

example:

private static class MyView extends View   
    {  
private Paint    mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);  
private Typeface mFace;  
public MyView(Context context)   
{  
super(context);   
mFace = Typeface.createFromAsset(getContext().getAssets(),"fonts/WINGDING.ttf");  
mPaint.setTextSize(32);  
}  
 @Override protected void onDraw(Canvas canvas)   
{   
mPaint.setTypeface(mFace);  
canvas.drawText("p p p p", 10, 200, mPaint);  
}  
} 



回答2:


I found this SO post that linked this font converter, http://www.freefontconverter.com.

I took my .ttf font that would not render and converted it to .ttf again (so .ttf to .ttf) and now it works! I hope this helps!



来源:https://stackoverflow.com/questions/10360616/drawing-text-on-canvas-with-wingding-ttf-font-is-not-working-on-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!