Lädt...

🔧 How to Implement Custom Fonts using Downloadable Google Fonts?


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

A step-by-step guide to implementing custom font by downloading (instead of manually copying) Google fonts and using them in your Jetpack Compose app.

Roboto is the default font used by Material 3. However, we often want to change it for our app. Instead of manually copying the font asset, you can now download the Google font asynchronously and use them in your Jetpack Compose app.

This is a quick guide to changing the default Roboto font to Arvo font.

1. Add the dependency

build.gradle.kts example

dependencies {
    /*...*/
    implementation("androidx.compose.ui:ui-text-google-fonts:1.3.2")
}

2. Copy font_cert.xml

Copy the font-cert.xml to your res\values folder.

3. Create Font Provider

In Type.kt, create the GoogleFont.Provider:

@OptIn(ExperimentalTextApi::class)
private val fontProvider = GoogleFont.Provider(
    providerAuthority = "com.google.android.gms.fonts",
    providerPackage = "com.google.android.gms",
    certificates = R.array.com_google_android_gms_fonts_certs
)

4. Create Google Font

Search the Google Font that you would like to use here. In this example, we're going to use Arvo font.

Not all fonts are supported, so you need to try them by yourself. If the font is not supported, it reverts to Roboto font.

In Type.kt, create the GoogleFont:

@OptIn(ExperimentalTextApi::class)
private val googleFont = GoogleFont("Arvo")

5. Create Font Family

In Type.kt, create the FontFamily:

@OptIn(ExperimentalTextApi::class)
private val fontFamily = FontFamily(
    Font(googleFont = googleFont, fontProvider = fontProvider)
)

6. Override the Typography

In Type.kt, overrides fontFamily in the Typography:

private val typography = Typography()
val Typography = Typography(
    displayLarge = typography.displayLarge.copy(fontFamily = fontFamily),
    displayMedium = typography.displayMedium.copy(fontFamily = fontFamily),
    displaySmall = typography.displaySmall.copy(fontFamily = fontFamily),

    headlineLarge = typography.headlineLarge.copy(fontFamily = fontFamily),
    headlineMedium = typography.headlineMedium.copy(fontFamily = fontFamily),
    headlineSmall = typography.headlineSmall.copy(fontFamily = fontFamily),

    titleLarge = typography.titleLarge.copy(fontFamily = fontFamily),
    titleMedium = typography.titleMedium.copy(fontFamily = fontFamily),
    titleSmall = typography.titleSmall.copy(fontFamily = fontFamily),

    bodyLarge = typography.bodyLarge.copy(fontFamily = fontFamily),
    bodyMedium = typography.bodyMedium.copy(fontFamily = fontFamily),
    bodySmall = typography.bodySmall.copy(fontFamily = fontFamily),

    labelLarge = typography.labelLarge.copy(fontFamily = fontFamily),
    labelMedium = typography.labelMedium.copy(fontFamily = fontFamily),
    labelSmall = typography.labelSmall.copy(fontFamily = fontFamily),
)

7. Use the New Typography

Make sure in your Theme.kt, the newly created Typography above is passed to the MaterialTheme(). Example:

@Composable
fun YourAppTheme(
    darkTheme: Boolean = isSystemInDarkTheme(),
    useSystemUIController: Boolean = true,
    content: @Composable () -> Unit
) {
    /*...*/
    MaterialTheme(
        colorScheme = colorScheme,
        shapes = Shapes,
        typography = Typography,
        content = content,
    )
}

Reference

Downloadable fonts (from the official doc)

Example

GitHub Repository: YouTubeWorkouts

Originally published at https://vtsen.hashnode.dev.

...

🔧 How to Implement Custom Fonts using Downloadable Google Fonts?


📈 69.45 Punkte
🔧 Programmierung

🔧 How to add Google Fonts and Custom Fonts to your Web App


📈 32.67 Punkte
🔧 Programmierung

🔧 How to Override Bootstrap Fonts with Custom Fonts


📈 30.81 Punkte
🔧 Programmierung

🔧 The Mystery of Fonts on iOS: Why Was My App Not Displaying Custom Fonts?


📈 30.81 Punkte
🔧 Programmierung

🔧 Implement React v18 from Scratch Using WASM and Rust - [27] Implement useTransition


📈 26.36 Punkte
🔧 Programmierung

🔧 Implement React v18 from Scratch Using WASM and Rust - [26] Implement React.lazy


📈 26.36 Punkte
🔧 Programmierung

🔧 Implement React v18 from Scratch Using WASM and Rust - [22] Implement memo


📈 26.36 Punkte
🔧 Programmierung

🔧 Implement React v18 from Scratch Using WASM and Rust - [20] Implement Context


📈 26.36 Punkte
🔧 Programmierung

🔧 Implement React v18 from Scratch Using WASM and Rust - [18] Implement useRef, useCallback, useMemo


📈 26.36 Punkte
🔧 Programmierung

🔧 Implement React v18 from Scratch Using WASM and Rust - [17] Implement Concurrent Mode


📈 26.36 Punkte
🔧 Programmierung

🔧 Implement React v18 from Scratch Using WASM and Rust [16] Implement React Noop


📈 26.36 Punkte
🔧 Programmierung

🔧 Implement React v18 from Scratch Using WASM and Rust - [14] Implement Scheduler


📈 26.36 Punkte
🔧 Programmierung

🔧 Implement React v18 from Scratch Using WASM and Rust - [13] Implement Lane and Batch Update


📈 26.36 Punkte
🔧 Programmierung

🔧 Implement React v18 from Scratch Using WASM and Rust - [12] Implement Update for Multi Node


📈 26.36 Punkte
🔧 Programmierung

🔧 Implement React v18 from Scratch Using WASM and Rust - [11] Implement Event System


📈 26.36 Punkte
🔧 Programmierung

🔧 Implement React v18 from Scratch Using WASM and Rust - [10] Implement Update for Single Node.


📈 26.36 Punkte
🔧 Programmierung

🐧 Can't load new fonts in programs, but they are there in fonts. (ubuntu)


📈 23.3 Punkte
🐧 Linux Tipps

🐧 Installing and using custom fonts on Linux Mint


📈 23.25 Punkte
🐧 Linux Tipps

🔧 Integrating Custom Fonts Using CSS Modules in Next.js


📈 23.25 Punkte
🔧 Programmierung

🔧 Add custom fonts to Next.js sites with Tailwind using next/font


📈 23.25 Punkte
🔧 Programmierung

🔧 How to Implement a Custom Lexer Using Tree Sitter in Python


📈 22.74 Punkte
🔧 Programmierung

📰 Implement Amazon SageMaker domain cross-Region disaster recovery using custom Amazon EFS instances


📈 22.74 Punkte
🔧 AI Nachrichten

📰 [Free & Downloadable] Cybersecurity Risk Management Template – 2024


📈 21.54 Punkte
📰 IT Security Nachrichten

🔧 OWASP Cheat Sheet for SDLC with Downloadable [XLS]


📈 21.54 Punkte
🔧 Programmierung

🕵️ [APPSEC-1995] Cross-site Scripting in Downloadable Products - 2


📈 21.54 Punkte
🕵️ Sicherheitslücken

📰 Free & Downloadable Cybersecurity Incident Response Plan Templates


📈 21.54 Punkte
📰 IT Security Nachrichten

📰 Free & Downloadable User Access Review Policy Template – 2024


📈 21.54 Punkte
📰 IT Security Nachrichten

🔧 Coral updates: Project tutorials, a downloadable compiler, and a new distributor


📈 21.54 Punkte
🔧 Programmierung

📰 Free & Downloadable Cybersecurity Risk Assessment Templates


📈 21.54 Punkte
📰 IT Security Nachrichten

📰 [Free & Downloadable] Endpoint Security Policy Template – 2024


📈 21.54 Punkte
📰 IT Security Nachrichten

📰 Scraping & General Safety. Downloadable Desktop App vs Website.


📈 21.54 Punkte
📰 IT Security Nachrichten