最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

android - Is there a way to implement Pinch to Zoom InZoom Out on a Jetpack Compose Text element without it overflowing the scre

programmeradmin0浏览0评论

I'm using the Zoomable library to Zoom In/Zoom Out a Text element in Jetpack Compose.

The problem is that when I enlarge the text size, the content overflows the limits of the screen.

Code:

@Composable
fun TextZoomable(text: AnnotatedString, onTap: (Offset) -> Unit) {
    val zoomState = rememberZoomState()
    Box(
        modifier = Modifier
            .fillMaxSize()
            .zoomable(
                zoomState = zoomState,
                onTap = onTap
            ),
    ) {
        Text(
            text = text,
        )
    }
}

Contextual use:

var text = AnnotatedString("")
//text is my some large content ...

Column {
    TextZoomable(onTap = onTap, text = text)
}

This is a screenshot before zooming out:

When I Zoom Out, this happens:

Is there a way to Zoom In/Out a Text element without it overflowing the screen boundaries?

Would it be possible to do this without using third-party libraries?

I'm using the Zoomable library to Zoom In/Zoom Out a Text element in Jetpack Compose.

The problem is that when I enlarge the text size, the content overflows the limits of the screen.

Code:

@Composable
fun TextZoomable(text: AnnotatedString, onTap: (Offset) -> Unit) {
    val zoomState = rememberZoomState()
    Box(
        modifier = Modifier
            .fillMaxSize()
            .zoomable(
                zoomState = zoomState,
                onTap = onTap
            ),
    ) {
        Text(
            text = text,
        )
    }
}

Contextual use:

var text = AnnotatedString("")
//text is my some large content ...

Column {
    TextZoomable(onTap = onTap, text = text)
}

This is a screenshot before zooming out:

When I Zoom Out, this happens:

Is there a way to Zoom In/Out a Text element without it overflowing the screen boundaries?

Would it be possible to do this without using third-party libraries?

Share Improve this question asked Feb 3 at 11:51 A. CedanoA. Cedano 98915 silver badges48 bronze badges 2
  • without it overflowing the screen boundaries - So do you want the Text to do line breaks accordingly, so that it fits the screen without overflowing? – BenjyTec Commented Feb 3 at 13:40
  • @BenjyTec Something like this, is my old implementation of a zoomable TextView. I would like to achieve something similar in Jetpack Compose. – A. Cedano Commented Feb 3 at 14:35
Add a comment  | 

1 Answer 1

Reset to default 2

The Zoomable Composable will not work in this case, as it performs any zoom on the graphical layer. What you actually want to do is to transform the value assigned as fontSize of your Text Composable.

You can use the transformable Modifier to achieve this:

@Composable
fun ZoomableText() {

    var textScale by remember { mutableFloatStateOf(1f) }
    val zoomState = rememberTransformableState { zoomChange, _, _ ->
        textScale *= zoomChange
    }

    Text(
        modifier = Modifier
            .fillMaxSize()
            .transformable(state = zoomState, lockRotationOnZoomPan = true)
            .verticalScroll(rememberScrollState()),
        text = LoremIpsum(2000).values.joinToString(),
        fontSize = (textScale * 16).sp,  // set base text size as needed
        lineHeight = (textScale * 16).sp  // should be same or more than fontSize
    )
}

Also make sure you are using the latest Compose Libraries, there were bugs in older library versions when combining transformable with verticalScroll. If you are using Compose BOM, use as of 01/2025

composeBom = "2025.01.01"

Output:

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论