एंड्रॉइड में TextView फॉन्ट फैमिली कस्टमाइज़ करना
यह ट्यूटोरियल सिस्टम फॉन्ट्स, कस्टम फॉन्ट्स (XML और प्रोग्रामेटिकली), और Downloadable Fonts API का उपयोग करके TextView का फॉन्ट फैमिली बदलने का तरीका बताता है।
विधि 1: सिस्टम फॉन्ट्स का उपयोग (XML)
एंड्रॉइड कई बिल्ट-इन फॉन्ट फैमिली प्रदान करता है:
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:fontFamily="cursive"
android:textSize="48sp" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World"
android:fontFamily="sans-serif-black"
android:textSize="48sp" />
उपलब्ध सिस्टम फॉन्ट फैमिली:
sans-serif(डिफ़ॉल्ट - Roboto)sans-serif-light,sans-serif-thin,sans-serif-medium,sans-serif-blacksans-serif-condensed,sans-serif-condensed-lightserif,serif-monospacemonospacecursive
विधि 2: कस्टम फॉन्ट्स (res/font फ़ोल्डर में) - API 26+
res/font/फ़ोल्डर बनाएं- .ttf या .otf फॉन्ट फ़ाइलें जोड़ें
- XML में उपयोग करें:
<TextView
android:fontFamily="@font/my_custom_font"
... />
विधि 3: प्रोग्रामेटिकली Typeface का उपयोग
import android.graphics.Typeface; TextView textView = findViewById(R.id.textView); // एसेट्स फोल्डर से फॉन्ट (fonts फोल्डर बनाएं assets/fonts/ में) Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/custom_font.ttf"); textView.setTypeface(typeface); // फॉन्ट वेट और स्टाइल के साथ textView.setTypeface(typeface, Typeface.BOLD_ITALIC);
विधि 4: Downloadable Fonts API (Google Fonts)
ऐप साइज़ बढ़ाए बिना Google Fonts का उपयोग करें:
<!-- res/font/roboto_regular.xml -->
<font-family xmlns:app="https://schemas.android.com/apk/res-auto"
app:fontProviderAuthority="com.google.android.gms.fonts"
app:fontProviderPackage="com.google.android.gms"
app:fontProviderQuery="Roboto"
app:fontProviderCerts="@array/com_google_android_gms_fonts_certs" />
विधि 5: Jetpack Compose में (आधुनिक तरीका)
Text(
text = "कस्टम फॉन्ट",
fontFamily = FontFamily(
Font(R.font.custom_font, FontWeight.Normal),
Font(R.font.custom_font_bold, FontWeight.Bold)
)
)
बेस्ट प्रैक्टिसेज
- फॉन्ट फ़ाइलों के लिए
res/font/का उपयोग करें (API 26+) - परफॉरमेंस के लिए फॉन्ट वेरिएंट्स को प्री-लोड करें
- लाइसेंसिंग प्रतिबंधों की जांच करें
- वेरिएबल फॉन्ट्स का उपयोग करके फ़ाइल साइज़ कम करें