swift 如何在fixedSize()VStack中实现多行文本?

t0ybt7op  于 2023-03-28  发布在  Swift
关注(0)|答案(1)|浏览(98)

我想创建泡沫聊天像WhatsApp的泡沫将根据其内容收缩.我尝试了几种解决方案,但没有一个工作.注意:在文本旁边,总是会有一些宽度为.infinity的内容(即分隔符)
1.
short text is okaylong text sholdnt be truncated

VStack {
                Text("I Want to create bubble chat like whatsapp where the bubble will shrink based on its content.")
                Divider()
                HStack {
                    Spacer()
                    TimeChatOverlay(Date())
                }
            }
            .frame(maxWidth: 280)
            .fixedSize(horizontal: true, vertical: false)
            .background(Color.red.opacity(0.1))

short text is okaybackground size is wrong

VStack {
                Text("I Want to create bubble chat like whatsapp where the bubble will shrink based on its content.")
                    .frame(maxWidth: 280)
                Divider()
                HStack {
                    Spacer()
                    TimeChatOverlay(Date())
                }
            }
            .fixedSize(horizontal: true, vertical: false)
            .background(Color.red.opacity(0.1))

我看到的是,问题来自Text(),如果使用fixedSize修饰符,它总是被截断。
我希望有人能帮助我,谢谢
固定大小容器内的多行文本

91zkwejq

91zkwejq1#

玩了一会儿。这个有用吗?

struct ContentView: View {
    var body: some View {
        VStack(alignment: .trailing) {
            Text("I Want to create bubble chat like whatsapp where the bubble will shrink based on its content. ")
            Divider()
            Text("\(Date().description)")
                .font(.footnote)

        }
        .padding(10)
        .background(Color.red.opacity(0.1))
        .frame(maxWidth: 280)
        .cornerRadius(15)
    }
}

相关问题