swift - 如何在 Swift UI 的 TextField 和 Text 组件中放置多行(断行)?

标签 swift swiftui

我正在尝试断开 TextField 和 Text 组件上的线条,这可能吗?

已经尝试过使用 frames 和 VStack 但没有成功

我正在使用的文本,我希望第一个中断:

VStack (alignment: .leading){
            Text(message.message)
                .bold()
                .foregroundColor(Color.black)
            Text(self.dateString)
                .font(.caption)
                .foregroundColor(Color.gray)
        }

我正在使用的 TextField:

HStack {
                TextField(self.$messageModel.message, placeholder: Text("Write a message..."),
                          onEditingChanged: { if $0 { self.kGuardian.showField = 0 }
                })
                .background(GeometryGetter(rect: $kGuardian.rects[0]))
                Button(action: {
                    self.messageModel.to = self.user.uid
                    self.messageModel.status = "read"
                    self.messageModel.date = Date().timeIntervalSince1970
                    ChatService.sendMessage(message: self.messageModel)
                    self.messageModel.message = ""
                }) {
                    Text("Send")
                }
            }

最佳答案

您可以对 TextView 使用 .lineLimit(nil)。它支持多行文本并根据需要分配空间。

struct StackOverFlow : View {
    var body: some View {
        Text("Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.")
       .lineLimit(nil)
    }
}

Without linelimit

With linelimit

关于swift - 如何在 Swift UI 的 TextField 和 Text 组件中放置多行(断行)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56949913/

相关文章:

swift - 带有 `isActive=true` 的嵌套导航链接未正确显示

audio - 如何等待SwiftUI中音频的完成?

swiftui - "\.self"在 swift/swiftUI 中实际上做了什么?

ios - swift : How to update UILabel from another class X: UIButton

带有 Optionals 的 Swift If 语句(来自计算器 Stanford/Youtube)

ios - UIButton 不能横向工作

Swift setToolbarHidden 不起作用

触摸时未检测到 Swift/Spritekit SKNode

ios - SwiftUI:避免使用 MKMapView+UIViewRepresentable 在 TabView 中重新创建/重新渲染 View

swift - 使用枚举迭代可选值