android 在Jetpack合成中,如何应用存储在资源中的字体/字样?

xriantvc  于 2023-02-27  发布在  Android
关注(0)|答案(3)|浏览(135)

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

gorkyyrv

gorkyyrv1#

是的,有一个默认方法,它将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
    )
)
kyvafyod

kyvafyod2#

1.在res中创建字体目录(res/font)
1.复制res中的.ttf字体/font
1.找到Type.kt文件(在ui/theme目录中)
1.在Type.kt中创建字体变量

val MyCustomFont = FontFamily(
    Font(R.font.regular),
    Font(R.font.bold,FontWeight.Bold)
)
  1. Type.kt文件中存在字体瓦尔,您可以通过在此值中输入defaultFontFamily = MyCustomFont来更改整个应用程序的字体系列
val Typography = Typography(
    defaultFontFamily = MyCustomFont,
    body1 = TextStyle(
        fontFamily = MyCustomFont2,
        fontWeight = FontWeight.Normal,
        fontSize = 16.sp
 ),

1.你可以设置字体系列到特定的排版如body 1,h1,h2,...
编辑:要更改材料3的字体,请在Type.kt中添加以下内容

bodyLarge = TextStyle(
    fontFamily = MyCustomFont,
    fontWeight = FontWeight.Normal,
    fontSize = 16.sp,
    lineHeight = 24.sp,
    letterSpacing = 0.5.sp
),
displayLarge= TextStyle(fontFamily = MyCustomFont),
displayMedium= TextStyle(fontFamily = MyCustomFont),
displaySmall= TextStyle(fontFamily = MyCustomFont),
headlineLarge= TextStyle(fontFamily = MyCustomFont),
headlineMedium= TextStyle(fontFamily = MyCustomFont),
headlineSmall= TextStyle(fontFamily = MyCustomFont),
titleLarge= TextStyle(fontFamily = MyCustomFont),
titleMedium= TextStyle(fontFamily = MyCustomFont),
titleSmall= TextStyle(fontFamily = MyCustomFont),
bodyMedium= TextStyle(fontFamily = MyCustomFont),
bodySmall= TextStyle(fontFamily = MyCustomFont),
labelLarge= TextStyle(fontFamily = MyCustomFont),
labelMedium= TextStyle(fontFamily = MyCustomFont),
labelSmall= TextStyle(fontFamily = MyCustomFont),
ozxc1zmp

ozxc1zmp3#

实际上,在composition中,通常有一个名为Typography.kt的类,它被MaterialTheme Composable使用。正如主题代码实验室中所描述的,正确的方法是修改这个类以向其添加字体。实际上,你可以创建自己的mAppTheme来覆盖Material的。
https://youtu.be/DDd6IOlH3io?t=6m27s
本视频演示如何实现自定义调色板,但也可以采用类似的方法来实现自定义版式。
查看JetSnack示例应用https://github.com/android/compose-samples

相关问题