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相关问答推荐

文本正在被切断在200%的屏幕比例在Jetpack Compose

关键字';在When Kotlin When-语句中

哪个更好? Integer.valueOf(readLine()) 或 readLine()!!.toInt()

循环中的每个元素都应填充行中可用的所有可用空间

Kotlin 中私有集的完整语法 struct 是什么?

如何在 kotlin 中通过反射设置委托属性值?

下拉通知面板时是否可以暂停Android中的任何视频(媒体播放器)应用程序?

如何使 TextInputEditText 只读?

Kotlin:如何在活页夹中返回正在运行的服务实例?

如何在 Kotlin 中判断数组类型(不是泛型类型)

无法为 retrofit2.Call 调用无参数构造函数

Mockito 的 argThat 在 Kotlin 中返回 null

在用Kotlin编写的Android库公共API中处理R8+JvmStatic Annotation+Lambda

Kotlin内联扩展属性

Kotlin 中更好的回调方法是什么?侦听器与高阶函数

在 Kotlin 函数上使用 Mokito anyObject() 时,指定为非 null 的参数为 null

如何将vararg作为数组传递给Kotlin中的函数?

将字符串编码为Kotlin中的UTF-8

旋转 kotlin 数组

Kotlin:在何时使用枚举