android set custom font to a paint

后端 未结 5 2305
难免孤独
难免孤独 2020-11-30 23:16

I want to draw a text to a paint. How to draw it with a custom font (ex Helvetica ) and bold also? I would preffer to use a system font and not create it fr

5条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-30 23:42

    If by "custom font" you mean a font that you are supplying as an asset, the following code should work:

    Typeface plain = Typeface.createFromAsset(assetManager, pathToFont); 
    Typeface bold = Typeface.create(plain, Typeface.DEFAULT_BOLD)
    Paint paint = new Paint();
    paint.setTypeface(bold);
    canvas.drawText("Sample text in bold",0,0,paint);
    

提交回复
热议问题