खोज…


अपने ऐप में एक कस्टम फॉन्ट डालना

  1. (प्रोजेक्ट फ़ोल्डर) पर जाएं
  2. फिर ऐप -> src -> main।
  3. मुख्य फ़ोल्डर में फ़ोल्डर 'एसेट्स -> फोंट' बनाएं।
  4. फोंट फ़ोल्डर में अपना 'fontfile.ttf' डालें।

एक फ़ॉन्ट को प्रारंभ करना

private Typeface myFont;

// A good practice might be to call this in onCreate() of a custom
// Application class and pass 'this' as Context. Your font will be ready to use
// as long as your app lives
public void initFont(Context context) {
    myFont = Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Light.ttf");
}

एक TextView में एक कस्टम फ़ॉन्ट का उपयोग करना

public void setFont(TextView textView) {
    textView.setTypeface(myFont);    
}

एक्सएमएल द्वारा TextView पर फ़ॉन्ट लागू करें (जावा कोड की आवश्यकता नहीं)

TextViewPlus.java:

public class TextViewPlus extends TextView {
    private static final String TAG = "TextView";

    public TextViewPlus(Context context) {
        super(context);
    }

    public TextViewPlus(Context context, AttributeSet attrs) {
        super(context, attrs);
        setCustomFont(context, attrs);
    }

    public TextViewPlus(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setCustomFont(context, attrs);
    }

    private void setCustomFont(Context ctx, AttributeSet attrs) {
        TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.TextViewPlus);
        String customFont = a.getString(R.styleable.TextViewPlus_customFont);
        setCustomFont(ctx, customFont);
        a.recycle();
    }

    public boolean setCustomFont(Context ctx, String asset) {
        Typeface typeface = null;
        try {
            typeface = Typeface.createFromAsset(ctx.getAssets(), asset);
        } catch (Exception e) {
            Log.e(TAG, "Unable to load typeface: "+e.getMessage());
            return false;
        }

        setTypeface(typeface);
        return true;
    }
}

attrs.xml: ( रिस / मान कहां रखें)

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="TextViewPlus">
        <attr name="customFont" format="string"/>
    </declare-styleable>
</resources>

कैसे इस्तेमाल करे:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:foo="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <com.mypackage.TextViewPlus
        android:id="@+id/textViewPlus1"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:text="@string/showingOffTheNewTypeface"
        foo:customFont="my_font_name_regular.otf">
    </com.mypackage.TextViewPlus>
</LinearLayout>

कैनवास पाठ में कस्टम फ़ॉन्ट

संपत्ति से अपने फ़ॉन्ट के साथ कैनवास में पाठ खींचना।

Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/SomeFont.ttf");
Paint textPaint = new Paint();
textPaint.setTypeface(typeface);
canvas.drawText("Your text here", x, y, textPaint);

कुशल टाइपफेस लोड हो रहा है

कस्टम फोंट लोड करने से खराब प्रदर्शन हो सकता है। मैं इस छोटे सहायक का उपयोग करने की सलाह देता हूं जो आपके पहले से उपयोग किए गए फोंट को हैशटेबल में सहेजता / लोड करता है।

public class TypefaceUtils {

private static final Hashtable<String, Typeface> sTypeFaces = new Hashtable<>();

/**
 * Get typeface by filename from assets main directory
 *
 * @param context
 * @param fileName the name of the font file in the asset main directory
 * @return
 */
public static Typeface getTypeFace(final Context context, final String fileName) {
    Typeface tempTypeface = sTypeFaces.get(fileName);

    if (tempTypeface == null) {
        tempTypeface = Typeface.createFromAsset(context.getAssets(), fileName);
        sTypeFaces.put(fileName, tempTypeface);
    }

    return tempTypeface;
}

}

उपयोग:

Typeface typeface = TypefaceUtils.getTypeface(context, "RobotoSlab-Bold.ttf");
setTypeface(typeface);

पूरी गतिविधि के लिए कस्टम फ़ॉन्ट

public class ReplaceFont {

public static void changeDefaultFont(Context context, String oldFont, String assetsFont) {
    Typeface typeface = Typeface.createFromAsset(context.getAssets(), assetsFont);
    replaceFont(oldFont, typeface);
}

private static void replaceFont(String oldFont, Typeface typeface) {
    try {
        Field myField = Typeface.class.getDeclaredField(oldFont);
        myField.setAccessible(true);
        myField.set(null, typeface);
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
}

फिर आपकी गतिविधि में, onCreate() विधि में:

// Put your font to assets folder...

ReplaceFont.changeDefaultFont(getApplication(), "DEFAULT", "LinLibertine.ttf");

एंड्रॉइड ओ में फोंट के साथ काम करना

Android O फोंट के साथ काम करने का तरीका बदलता है।

एंड्रॉइड ओ एक नई सुविधा पेश करता है, जिसे एक्सएमएल में फोंट कहा जाता है, जो आपको संसाधनों के रूप में फोंट का उपयोग करने की अनुमति देता है। इसका मतलब है, कि फोंट को संपत्ति के रूप में बंडल करने की कोई आवश्यकता नहीं है। फ़ॉन्ट्स अब एक आर फ़ाइल में संकलित हैं और सिस्टम में संसाधन के रूप में स्वचालित रूप से उपलब्ध हैं।

एक नया फ़ॉन्ट जोड़ने के लिए, आपको निम्नलिखित कार्य करने होंगे:

  • एक नया संसाधन निर्देशिका बनाएँ: res/font
  • इस फ़ॉन्ट फ़ोल्डर में अपनी फ़ॉन्ट फ़ाइलों को जोड़ें। उदाहरण के लिए, myfont.ttf को जोड़कर, आप R.font.myfont माध्यम से इस फ़ॉन्ट का उपयोग कर पाएंगे।

आप निम्न XML फ़ाइल को res/font निर्देशिका में जोड़कर अपना स्वयं का फ़ॉन्ट परिवार भी बना सकते हैं:

<?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/lobster_regular" />
    <font
        android:fontStyle="italic"
        android:fontWeight="400"
        android:font="@font/lobster_italic" />
</font-family>

आप फ़ॉन्ट फ़ाइल और फ़ॉन्ट परिवार फ़ाइल दोनों का उपयोग एक ही तरीके से कर सकते हैं:

  • एक XML फ़ाइल में, android:fontFamily का उपयोग करके android:fontFamily विशेषता, उदाहरण के लिए इस तरह:

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

    या इस तरह:

    <style name="customfontstyle" parent="@android:style/TextAppearance.Small">
        <item name="android:fontFamily">@font/myfont</item>
    </style>
    
  • अपने कोड में , कोड की निम्नलिखित पंक्तियों का उपयोग करके:

    Typeface typeface = getResources().getFont(R.font.myfont);
    textView.setTypeface(typeface);
    


Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow