4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Jetpack ComposeのTextで表示されている文字列の行数を取得する

Posted at

TextのonTextLayoutのラムダに入ってくる TextLayoutResultlineCount を使用すると表示される文字列の行数を取得することが出来る

Text(
    onTextLayout = { result ->
        Timber.d("onTextLayout=${result.lineCount}")
    }
)

なので、rememberの値と lineCount の値を使えば改行されるような文字列が入ってきたら非表示にするとかが出来る

@Composable
fun Hoge(
  text: String,
) {
    var isMultiline by remember { mutableStateOf(false) }
    // 改行されたら非表示にする
    if (isMultiline.not()) {
        Text(
            text = text,
            onTextLayout = { result ->
              isMultiline = result.lineCount > 1
            }
        )
    }
}
4
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?