我正在做我的第一个jetpack撰写项目,在标题屏幕和其他一些部分中,我希望有由一个单词组成并且比通常文本更大的“标题”文本。当我在模拟器上测试它们时,一切都很好,但是当我尝试在物理设备上测试时,最后几个字符出现在下一行中,因为它们不适合。所以我的问题是,如何调整文本大小,使其适合每台设备上的一行。
这是我的排版:
val Typography = Typography(
headlineMedium = TextStyle(
fontFamily = customFontFamily,
fontWeight = FontWeight.Normal,
fontSize = 36.sp,
lineHeight = 36.sp,
letterSpacing = 1.sp
),
labelLarge = TextStyle(
fontFamily = customFontFamily,
fontWeight = FontWeight.Normal,
fontSize = 32.sp,
lineHeight = 32.sp,
letterSpacing = 0.5.sp
),
labelMedium = TextStyle(
fontFamily = customFontFamily,
fontWeight = FontWeight.Normal,
fontSize = 28.sp,
lineHeight = 28.sp,
letterSpacing = 0.5.sp
),
labelSmall = TextStyle(
fontFamily = customFontFamily,
fontWeight = FontWeight.Medium,
fontSize = 24.sp,
lineHeight = 24.sp,
letterSpacing = 0.5.sp
),
titleMedium = TextStyle(
fontFamily = stylizedCustomFontFamily,
fontWeight = FontWeight.Normal,
fontSize = 36.sp,
lineHeight = 50.sp,
letterSpacing = 6.sp
),
titleSmall = TextStyle(
fontFamily = stylizedCustomFontFamily,
fontWeight = FontWeight.Normal,
fontSize = 32.sp,
lineHeight = 40.sp,
letterSpacing = 4.sp
),
titleLarge = TextStyle(
fontFamily = stylizedCustomFontFamily,
fontWeight = FontWeight.Normal,
fontSize = 40.sp,
lineHeight = 60.sp,
letterSpacing = 8.sp
), )
使用 BoxWithConstraints 调整字体大小并获取屏幕大小限制。为了测量目的,您可以使用 TextMeasurer 或 onTextLayout 回调来确定您的文本是否适合可用宽度,并设置最小字体大小以避免文本变得不可读。