9

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,470
  • 8
  • 60
  • 101
NadeemYousaf
  • 223
  • 1
  • 3
  • 11

3 Answers3

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,494
  • 1
  • 23
  • 30
Zankhna
  • 4,470
  • 8
  • 60
  • 101
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
  • 19,210
  • 15
  • 86
  • 129
0

Use the next:

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