10

hi i want to change my font size by using paint , canvas in android. My code is here. how can i do this ?

public class MainActivity extends Activity 

{
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}
Canvas canvas = new Canvas();
Typeface tf = Typeface.createFromAsset(getAssets(), "RECOGNITION.ttf");
     Paint paint = new Paint();
     paint.setTypeface(tf);
     canvas.drawText("Lorem ipsum", 0, 0, paint);


}
}

can any body help me to solve problem ? i read some tutorials but not under stand . i have read some post of Stack ,facing some problems.

Zankhna
  • 4,570
  • 9
  • 62
  • 103
NadeemYousaf
  • 233
  • 1
  • 3
  • 12

4 Answers4

26

create "fonts" folder under "assets" folder. After that put your font file in "fonts" folder and write below code.

   Typeface tf =Typeface.createFromAsset(getAssets(),"fonts/YOURFONT.ttf");
   Paint paint = new Paint();
   paint.setTypeface(tf);
   canvas.drawText("Sample text in bold RECOGNITION",0,0,paint);
lxknvlk
  • 2,744
  • 1
  • 27
  • 32
Zankhna
  • 4,570
  • 9
  • 62
  • 103
1

Use this:

   Typeface tf = Typeface.createFromAsset(getAssets(),"RECOGNITION.ttf");
   Paint paint = new Paint();
   paint.setTypeface(tf);
   canvas.drawText("Sample text in bold RECOGNITION",0,0,paint);
Shailendra Madda
  • 20,649
  • 15
  • 100
  • 138
0

Use the next:

 Paint paint = new Paint();
 paint.setTypeface(tf);
 paint.setTextSize(yourTextSize);
 canvas.drawText("Lorem ipsum", 0, 0, paint);
JagrDev
  • 107
  • 4
0

Full setup text in a custom view:

    TextPaint textPaint = new TextPaint();
    Typeface tf =Typeface.createFromAsset(getContext().getAssets(),"fonts/BungeeSpice-Regular.ttf");
    textPaint.setTypeface(tf);
    textPaint.setStrokeWidth(7);
    textPaint.setTextSize(Tool.convertSpToPx(getContext(), 30));
    textPaint.setAntiAlias(true);
    textPaint.setPathEffect(null);
    textPaint.setColor(getResources().getColor(R.color.green, null));
dotrinh PM
  • 903
  • 1
  • 7
  • 19