Search code examples
javaandroidtextviewtypeface

How to set typeface to CustomView's Text in Android?


I am trying to draw a an arc text in Android. This is my CustomView:

public class GraphicsView extends View {
    private static final String MY_TEXT = "xjaphx: Draw Text on Curve";
    private Path mArc;

    private Paint mPaintText;

    public GraphicsView(Context context) {
      super(context);     

      mArc = new Path();
      RectF oval = new RectF(50,100,200,250);;
      mArc.addArc(oval, -180, 200);          
      mPaintText = new Paint(Paint.ANTI_ALIAS_FLAG);
      mPaintText.setStyle(Paint.Style.FILL_AND_STROKE);
      mPaintText.setColor(Color.WHITE);
      mPaintText.setTextSize(20f);

    }

    @Override
    protected void onDraw(Canvas canvas) {
      canvas.drawTextOnPath(MY_TEXT, mArc, 0, 20, mPaintText);      
      invalidate();
    }
  }

I want to change MY_TEXT Typeface. How can I do that? Thanks.


Solution

  • You can set your custom typeface to paint by this way

    Typeface plain = Typeface.createFromAsset(assetManager, pathToFont); 
    Typeface bold = Typeface.create(plain, Typeface.DEFAULT_BOLD)
    Paint paint = new Paint();
    paint.setTypeface(bold);