114

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.

Nikhil
  • 16,194
  • 20
  • 64
  • 81
Buda Gavril
  • 21,409
  • 40
  • 127
  • 196

7 Answers7

183

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);
stkent
  • 19,772
  • 14
  • 85
  • 111
Tony the Pony
  • 40,327
  • 71
  • 187
  • 281
  • Helvetica is not installed, but I've used some installed fonts to compare. If I'm using a font from assets, do you have any idea how to set bold? – Buda Gavril May 18 '11 at 13:07
  • 6
    Try: `Typeface plain = Typeface.createFromAsset(assetManager, pathToFont); Typeface bold = Typeface.create(plain, Typeface.DEFAULT_BOLD)`. – Tony the Pony May 18 '11 at 13:54
  • 2
    @TonythePony your code not work for me. Typeface fontFace=Typeface.createFromAsset(getAssets(),"fonts/comic.TTF"); Typeface face = Typeface.create(fontFace, Typeface.BOLD); Paint paint = new Paint(); paint.setTextAlign(Paint.Align.CENTER); paint.setColor(Color.WHITE); paint.setTextSize(10); paint.setTypeface(face); paint.setFlags(Paint.ANTI_ALIAS_FLAG); – Ashish Dwivedi May 24 '12 at 05:54
  • 1
    Did you try `getContext().getAssets()` ? – Wesley Jul 05 '12 at 21:01
  • `Typeface.create(plain, Typeface.DEFAULT_BOLD);` shows error: cannot resolve method create(android.graphics.TypeFace, android.graphics.TypeFace) – lxknvlk Sep 25 '15 at 12:20
  • 2
    `Typeface.DEFAULT_BOLD` gave me issues as well but changing to `Typeface.BOLD` worked – CrandellWS Jan 28 '16 at 22:14
  • That is bacause `Typeface.DEFAULT_BOLD` is a family, not style and should be used as first param – schmidt9 Mar 01 '16 at 06:25
  • mPaint.setFakeBoldText(true); bold can be done this way as well – Rajesh N Oct 10 '17 at 06:57
  • How do I load the fonts using the new support library feature : https://developer.android.com/guide/topics/ui/look-and-feel/downloadable-fonts.html ? – android developer Dec 07 '17 at 08:38
66

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

Sebastian Helzer
  • 839
  • 7
  • 12
19

Use this for paint class:

 Paint paint = new Paint();
   paint.setTypeface(Typeface.create("Arial",Typeface.ITALIC));
gofr1
  • 15,741
  • 11
  • 42
  • 52
Pritam
  • 361
  • 3
  • 5
10

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

Neil D'Souza
  • 236
  • 3
  • 7
5

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.

Paweł Rubin
  • 2,030
  • 1
  • 14
  • 25
  • 1
    This is the simplest solution, but it only works with SDK 26+. You can also use `ResourcesCompat.getFont(context, R.font.font_name)` when targeting older Android versions. – Slav Nov 28 '20 at 21:02
3

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);
James Ryan
  • 61
  • 1
2

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)
Cube
  • 362
  • 4
  • 8