Sercan Samet Savran
Sercan Samet Savran

Reputation: 945

Canvas drawText (font) import Times font?

I'm trying to use Times as font for the drawText()-method of the Canvas element. However couldn't find a solution yet.

There is the possibility to set fonts like 'sans-serif' or 'casual' by using following code:

paint.setTypeface(Typeface.create("casual",Typeface.NORMAL));

However trying to use Times or Arial etc. doesn't work. Do I have to import these fonts first by myself?

Would appreciate if You have a solution for this. Thank You in advance!

Upvotes: 0

Views: 416

Answers (2)

Sercan Samet Savran
Sercan Samet Savran

Reputation: 945

This helped me to solve it: 1 - Creating a font directory in resources (res) folder. 2 - Drag & Drop .tff file in the font directory (example.tff). And do following:

Typeface myFont = ResourcesCompat.getFont(this, R.font.times);

Source (see accepted answer): Link

Upvotes: 0

Genesis
Genesis

Reputation: 368

First you need to create an 'assets' folder under the 'main' folder and put a font file like .ttf file in it.

assets folder path

this is casual font download link

https://befonts.com/casual-font.html

I hope this is the answer you want. thank you

example)

Typeface mTfRegular = Typeface.createFromAsset(getContext().getAssets(), "OpenSans-Regular.ttf");
setTypeface(mTfRegular)

Another easy way to do this is to use the font family.

this way,

Create a folder called font in the /app/src/main/res/ path, put the font file inside, and write the contents of the Font Family in xml in /app/src/main/res/xml, and then apply the TextView or EditText you want to apply. Apply it using the android: fontFamily property.

Writing the Font Family in XML,

<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:android="http://schemas.android.com/apk/res/android">
    <font
        android:fontStyle="normal"
        android:fontWeight="400"
        android:font="@font/casual_regular" />
    <font
        android:fontStyle="italic"
        android:fontWeight="400"
        android:font="@font/casual_italic" />
</font-family>

Apply Fonts to FontView Using TextView,

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:fontFamily="@font/casual"/>

Upvotes: 1

Related Questions