Tech Sheets

Set font size in Jetpack Compose

最終更新日:2024-07-10

If you want to set font size in Text Composable, pass it with sp degree to fontSize param.

@Composable
fun Greeting(name: String) {
    Row {
        Text(text = "ねぇ")
        Text(text = "$name!", fontSize = 25.sp)
    }
}

The preview will look like this:

Whati is .sp?

25.sp is an expression that is not often seen in normal programming. Let's take a look at the contents.

val Int.sp: TextUnit get() = pack(UNIT_TYPE_SP, this.toFloat())

It is defined as an extension property of type Int. We don't recommend to investigate pack() function..

一覧に戻る