How does android set its own font in canvas?

Hi, I want to change my font size 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 solve the problem? I read some lessons, but did not stand. I read some stack posts, running into some problems.

+6
source share
3 answers

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

  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); 
+19
source

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);
+1
source

Use the following:

  Paint paint = new Paint(); paint.setTypeface(tf); paint.setTextSize(yourTextSize); canvas.drawText("Lorem ipsum", 0, 0, paint); 
0
source

Source: https://habr.com/ru/post/959022/


All Articles