android set custom font to a paint

AndroidFontsSetPaint

Android Problem Overview


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 from assets. Thanks.

Android Solutions


Solution 1 - Android

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);

Solution 2 - Android

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

Solution 3 - Android

Use this for paint class:

 Paint paint = new Paint();
   paint.setTypeface(Typeface.create("Arial",Typeface.ITALIC));

Solution 4 - Android

If you already have a font in use and want to use a bold version of that you can do this.

currentPainter = new Paint(Paint.ANTI_ALIAS_FLAG);
currentPainter.setColor(Color.WHITE);
currentPainter.setTextSize(Utils.sp2px(getResources(), 14)); // set font size
Typeface currentTypeFace =   currentPainter.getTypeface();
Typeface bold = Typeface.create(currentTypeFace, Typeface.BOLD);
currentPainter.setTypeface(bold);

I used the answer above, but this modification was necessary for me - so just thought I'd mention it

Solution 5 - Android

If you want to use a font from resources (Kotlin):

val textPaint = TextPaint()
textPaint.typeface = resources.getFont(R.font.font_name)

This might not be related to the question, but this is what I was looking for - maybe somebody would need it too.

Solution 6 - Android

with FontUtils kotlin object

object FontUtils {

    private const val FONT_PATH_LATO_REGULAR = "lato_regular.ttf"

    fun getDefaultTypeface(context: Context): Typeface {
        return Typeface.createFromAsset(context.assets, FONT_PATH_LATO_REGULAR)
    }
}

then you can use it as:

paint.typeface = FontUtils.getDefaultTypeface(context)

Solution 7 - Android

The custom font must be placed in the assets folder.

Maybe the following code can help you

Paint p = new Paint();
//Set font
Typeface plain = Typeface.createFromAsset(context.getAssets(), "custom_font.ttf");
p.setTypeface(plain);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBuda GavrilView Question on Stackoverflow
Solution 1 - AndroidTony the PonyView Answer on Stackoverflow
Solution 2 - AndroidSebastian HelzerView Answer on Stackoverflow
Solution 3 - AndroidPritamView Answer on Stackoverflow
Solution 4 - AndroidNeil D'SouzaView Answer on Stackoverflow
Solution 5 - AndroidPaweł RubinView Answer on Stackoverflow
Solution 6 - AndroidCubeView Answer on Stackoverflow
Solution 7 - AndroidJames RyanView Answer on Stackoverflow