Vin*_*hwa 3 android android-jetpack-compose
在 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)
Run Code Online (Sandbox Code Playgroud)
现在访问这样的字体!
@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
)
)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6523 次 |
| 最近记录: |