I want to create a generic FontFamily. The font is...
# compose-android
f
I want to create a generic FontFamily. The font is stored in the assets directory, so I thought of the following code, is there any other better way?
Copy code
val XXXFontFamily: FontFamily
    @Composable
    get() {
        val context = LocalContext.current
        return remember(context) { FontFamily(Font(path = "font/xxx.ttf", assetManager = context.assets)) }
    }

@Composable
fun Component() {
    Column {
        Text(text = "Hello")
        Text(text = "World", fontFamily = XXXFontFamily)
    }
}
m
Maybe better to define something like:
Copy code
@Composable
fun rememberXXXFontFamily(): FontFamily {
    val context = LocalContext.current
    return remember(context) {
        FontFamily(Font(path = "font/xxx.ttf", assetManager = context.assets))
    }
}
Actually no need to include
context
in the
remember
because
context.assets
never changes.
f
Maybe, I don't even want to wrap it in
remember
. It would be great if it could be like a resource, but there seems to be no way to do it.
Copy code
@Stable
val XXXFontFamily: FontFamily = FontFamily(Font(<http://R.font.xxx|R.font.xxx>))
m
I store mine in a singleton provided via dependency injection. It can expose the
FontFamily
to the UI