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
    )
}

推荐答案

If you are using default theme it's in

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
    )
    */
)

However, if you apply any other theme or get Text from another library can get size of your text using onTextLayout callback which returns a lot of data about your text.

Without removing default padding on top or bottom of your font size won't be able to get correct height in sp. You get the height of Text in sp with the method below. I tried using LineHeightStyle Api also need to check lineHeight either for correct results.

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)
}

Kotlin相关问答推荐

Kotlin 插件之间的区别

如何将字符串格式化为电话号码kotlin算法

使用 LazyListScope 嵌套可组合项

多次运行espresso测试

如何为 material.Slider 视图创建绑定适配器?

Kotlin 和 Java 字符串拆分与正则表达式的区别

如何在MVVM架构中观察RecyclerView适配器中的LiveData?

Tornadofx - 如何在每个实例上将参数传递给 Fragment

使用导航组件在BottomNavigationView中传递参数

如何在Android Studio 4.1中默认启用Kotlin Android扩展

在 Kotlin 中编写一个等于 Int.MIN_VALUE 的十六进制整数文字

如何判断数据是否插入到房间数据库中

在 Kotlin 中,如何声明一个构造函数参数为零的数据类?

Android 应用程序中的 Kotlin 编译器:Connection refused to host: 127.0.0.1

期待 Kotlin 中的成员声明

使用 Gradle Kotlin DSL 包含脚本

类型只能包含一个 @Inject 构造函数

maven-source-plugin 不适用于 kotlin

Room数据库 onConflict = OnConflictStrategy.REPLACE 不工作

我们可以将 java 方法传递给 () -> Unit 类型的 kotlin 函数吗?