我在Android中通过空撰写活动向导创建了一个新项目。
代码A是由Android生成的代码。
Text(text = "Hello $name!")
显示一个默认字体样式的文本,我希望得到文本的大小和单位,如16 as,在哪里可以找到这些信息?
码A
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MyApplicationTheme {
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colors.background
) {
Greeting("Android")
}
}
}
}
}
@Composable
fun Greeting(name: String) {
Text(text = "Hello $name!")
}
@Composable
fun MyApplicationTheme(
darkTheme: Boolean = isSystemInDarkTheme(),
content: @Composable () -> Unit
) {
val colors = if (darkTheme) {
DarkColorPalette
} else {
LightColorPalette
}
MaterialTheme(
colors = colors,
typography = Typography,
shapes = Shapes,
content = content
)
}
发布于 2022-08-05 03:58:36
如果您使用的是默认主题,则在
MaterialTheme(
colorScheme = colorScheme,
typography = Typography,
content = content
)
// Set of Material typography styles to start with
val Typography = Typography(
bodyLarge = TextStyle(
fontFamily = FontFamily.Default,
fontWeight = FontWeight.Normal,
// HERE
fontSize = 16.sp,
lineHeight = 24.sp,
letterSpacing = 0.5.sp
)
/* Other default text styles to override
titleLarge = TextStyle(
fontFamily = FontFamily.Default,
fontWeight = FontWeight.Normal,
fontSize = 22.sp,
lineHeight = 28.sp,
letterSpacing = 0.sp
),
labelSmall = TextStyle(
fontFamily = FontFamily.Default,
fontWeight = FontWeight.Medium,
fontSize = 11.sp,
lineHeight = 16.sp,
letterSpacing = 0.5.sp
)
*/
)
但是,如果您应用任何其他主题或从另一个库获取文本,则可以使用onTextLayout回调获得文本大小,这将返回大量有关文本的数据。
如果不删除默认填充,字体大小的顶部或底部将无法在sp中获得正确的高度。下面的方法可以得到sp中文本的高度。我尝试使用LineHeightStyle Api,也需要检查lineHeight是否有正确的结果。
Column {
var heightInPx by remember { mutableStateOf(0f) }
var info by remember { mutableStateOf("") }
val heightInSp = LocalDensity.current.run { heightInPx.toSp() }
Text("Hello World",
modifier = Modifier
.border(1.dp, Color.Green)
.fillMaxWidth(),
style = TextStyle(
fontSize = 40.sp,
platformStyle = PlatformTextStyle(
includeFontPadding = false
),
lineHeightStyle = LineHeightStyle(
alignment = LineHeightStyle.Alignment.Top,
trim = LineHeightStyle.Trim.Both
)
),
onTextLayout = { result: TextLayoutResult ->
val cursorRect = result.getCursorRect(0)
info = "firstBaseline: ${result.firstBaseline}, " +
"lastBaseline: ${result.lastBaseline}\n" +
"cursorRect: $cursorRect\n" +
"getLineBottom: ${result.getLineBottom(0)}, " +
"getBoundingBox: ${result.getBoundingBox(0)}"
heightInPx =
result.size.height.toFloat()
}
)
Text("Height in px: $heightInPx, height in sp: $heightInSp")
Text(info)
}
发布于 2022-08-05 02:03:48
你可以在这里查一下。
https://developer.android.com/jetpack/compose/text
更改文本大小和字体样式
Text("Hello World", fontSize = 30.sp,fontStyle = FontStyle.Italic)
文本具有一个fontFamily参数,允许设置可组合中使用的字体。默认情况下,包括衬线、无衬线、单空格和草书字体系列.
您可以使用TextLayoutResult获取前端样式。
字体样式为TextLayoutInput -> TextStyle
Text("Hello World",
onTextLayout = { result: TextLayoutResult ->
Log.d(TAG, "FrontSize: "+ result.layoutInput.style.fontSize)
Log.d(TAG, "FrontStyle: "+ result.layoutInput.style.toString())
})
发布于 2022-08-05 09:43:16
正如您在Text
可组合源代码中所看到的那样,当您显式地没有指定fontSize
或style
(您没有指定)时,它会从默认样式(即LocalTextStyle.current
)中获取字体大小。
现在,LocalTextStyle
可以在代码中包含不同的样式,您可以自己重写它,方法如下:
CompositionLocalProvider(LocalTextStyle provides TextStyle(...)) {
Text() // will use specified TextStyle
}
许多材料组件都是这样做的,例如TopAppBar
和Button
,所以您不必关心任何事情,并且总是使用正确的样式。
最后,您的样式来自可组合的MaterialTheme
,这段代码在哪里:
ProvideTextStyle(value = typography.body1) {
content()
}
因此,您的字体大小来自Typography.body1
文本样式。
https://stackoverflow.com/questions/73243722
复制相似问题