gpt4 book ai didi

android - 在 Jetpack Compose 中,如何应用存储在 Assets 中的字体/字体?

转载 作者:行者123 更新时间:2023-12-05 00:15:15 25 4
gpt4 key购买 nike

在 Jetpack compose 中,文档建议使用 font-family 属性应用字体并引用存储在 res/fonts 文件夹中的字体文件。是否也可以使用存储在 assets/ 下的字体文件?

最佳答案

是的,有一个默认方法将 AssetManager 作为参数:

/**
* Create a Font declaration from a file in the assets directory. The content of the [File] is
* read during construction.
*
* @param assetManager Android AssetManager
* @param path full path starting from the assets directory (i.e. dir/myfont.ttf for
* assets/dir/myfont.ttf).
* @param weight The weight of the font. The system uses this to match a font to a font request
* that is given in a [androidx.compose.ui.text.SpanStyle].
* @param style The style of the font, normal or italic. The system uses this to match a font to a
* font request that is given in a [androidx.compose.ui.text.SpanStyle].
*/
@ExperimentalTextApi
@OptIn(InternalPlatformTextApi::class, ExperimentalTextApi::class)
@Stable
fun Font(
assetManager: AssetManager,
path: String,
weight: FontWeight = FontWeight.Normal,
style: FontStyle = FontStyle.Normal
): Font = AndroidAssetFont(assetManager, path, weight, style)

现在访问这样的字体!

@OptIn(ExperimentalTextApi::class)
@Composable
fun fontFamily() = FontFamily(
Font(LocalContext.current.assets,"myfont.ttf")
)

@Composable
fun typography() = Typography(

h1 = TextStyle(
fontFamily = fontFamily(),
fontWeight = FontWeight.Bold,
fontSize = 30.sp
)
)

关于android - 在 Jetpack Compose 中,如何应用存储在 Assets 中的字体/字体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68144459/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com