I create a new project by Empty Compose Activity wizard in Android Studio.
The Code A is generated code by Android Studio.
Text(text = "Hello $name!")
displays a text with default font style, I hope to get the size and unit of the text, such as 16sp, where can I find these informations?
Code 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
)
}
You can check it here.
https://developer.android.com/jetpack/compose/text
Change text size and font style
Text("Hello World", fontSize = 30.sp,fontStyle = FontStyle.Italic)
Text has a fontFamily parameter to allow setting the font used in the composable. By default, serif, sans-serif, monospace and cursive font families are included.
You can use the TextLayoutResult to get the front style
The font style is in TextLayoutInput -> TextStyle
Text("Hello World",
onTextLayout = { result: TextLayoutResult ->
Log.d(TAG, "FrontSize: "+ result.layoutInput.style.fontSize)
Log.d(TAG, "FrontStyle: "+ result.layoutInput.style.toString())
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With