android set custom font to a paint

后端 未结 5 2308
难免孤独
难免孤独 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 you are using Android's new Fonts in XML for your fonts, then to get the typeface used for paint you can use:

    val customTypeface = ResourcesCompat.getFont(context, R.font.myfont)
    

    or if your min Android API >= 26

    val customTypeface = resources.getFont(R.font.myfont)
    

    Then to apply it to your paint object:

    mTextPaint.typeface = customTypeface
    

    For more info check out https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml#fonts-in-code

提交回复
热议问题