使文本元素始终为“两行" [英] Make a text element always "two lines"

查看:22
本文介绍了使文本元素始终为“两行"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的垂直堆栈

I have a vertical stack like this

VStack
  Text(lineA)
    .lineLimit(2)

  Text(lineB)

  Text(lineC)
}

别问我为什么.我的老板决定他们希望第一个 Text 总是两行,即使 lineA 变量没有使文本增长到两行的字符.

Don't ask me why. My bosses decided that they want the first Text always with two lines, even if the lineA variable has no characters to make the Text grow to two lines.

如果是两行,有没有办法让第一个文本渲染,如果超过两行,仍然显示沉默?

Is there a way to make the first Text to render if it is two lines and to still show the reticences if there is more than 2 lines?

我可以看到使用框架来做到这一点,这是一种自杀,将高度固定为可能因设备而异的点值.

I can see using a frame to do that, a kind of suicide, fixing the height to a value in points that may vary from device to device.

有没有更不致命的解决方案?

Is there a less lethal solution?

推荐答案

这里是可能的解决方案(使用 Xcode 12.4/iOS 14.4 测试)

Here is possible solution (tested with Xcode 12.4 / iOS 14.4)

var body: some View {
    VStack {
        Text("X\nX").foregroundColor(.clear)
            .frame(maxWidth: .infinity)
            .overlay(
                Text("sdfasdfasdfasd asd f asdf asd f asdf asd fas df ")
//              Text("sdfasdfasdfasd asd f asdf asd f asdf asd fas df asdf asd fa sdf asdfasdfasd asdf as df asdfasdf asdf")
            , alignment: .top)

        Text("lineB")
        Text("lineC")
    }
}

这篇关于使文本元素始终为“两行"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆