文本不会在快速用户界面中自动换行

时间:2019-06-08 11:27:55

标签: ios swift swiftui

即使设置.lineLimit(nil)后,文本也不会自动换行。

var body: some View {
    VStack(alignment: .center) {
        Text("SwiftUI is a modern way to declare user interfaces for any Apple platform. ")
            .font(.title)
            .color(.red)
            .lineLimit(nil)
        Text("Create beautiful, dynamic apps faster than ever before.")
            .font(.system(size: 20))
            .lineLimit(nil)
    }.padding(EdgeInsets(top: 0, leading: 10, bottom: 0, trailing: 10))
}

enter image description here

8 个答案:

答案 0 :(得分:14)

此处在多个答案中都找到了Xcode 11.3 / Swift 5.1的完整解决方案。

Matteo Pacini's answer中找到发生这种情况的原因的解释:使用预定义的.font(.title).font(.headline)等似乎带来了以下行为:Text视图调整大小以使其始终为椭圆形而不是环绕。但是,仅切换到.body似乎并不是最好的解决方法。

最好的解决方法是在Sharpienero's answer中:将.fixedSize(horizontal: false, vertical: true)添加到您的Text视图中。这告诉Text视图不要对NOT椭圆进行自定义的水平调整水平逻辑,这使其遵循我们都习惯的标准规则。

感谢他们俩!

答案 1 :(得分:5)

在花费大量时间处理此类错误之后,我不能100%地确定这是lineLimit问题。在撰写本文时,我发现具有更复杂视图的解决方案是以下代码片段,以防止换行:

.fixedSize(horizontal: false, vertical: true)

这应该防止解释器垂直折叠文本,在文本末尾留下“ ...”。

我希望这对某人有帮助。

答案 2 :(得分:3)

尝试将第二个文本的lineLimit更改为数字而不是nil:

VStack(alignment: .leading) {
  Text("SwiftUI is a modern way to declare user interfaces for any Apple platform. ")
    .font(.title)
    .color(.red)
    .lineLimit(nil)
  Text("Create beautiful, dynamic apps faster than ever before.")
    .font(.system(size: 20))
    .lineLimit(2)
 }.padding(EdgeInsets(top: 0, leading: 10, bottom: 0, trailing: 10))

结果:

enter image description here

答案 3 :(得分:3)

对于我的问题,我有这样的设置:

public var body: some View {
    Form {
        Text("Lorem ipsum dolor sit amet, consectetur adipiscing elit.")
            .font(.title)

        Spacer()
            .fixedSize()

        Text("""
            Integer ut orci odio. Proin cursus ut elit eget rutrum. Nunc ante sem, euismod sed purus sed, tempus elementum elit. Phasellus lobortis at arcu quis porta. Cras accumsan leo eu tempus molestie. Suspendisse vulputate diam ipsum, et tristique lorem porta et. Pellentesque sodales est id arcu luctus venenatis.

            Vestibulum non magna lorem. In tincidunt aliquet nunc, sit amet pharetra neque hendrerit id.

            Cras sed!
            """)

        NativeButton("OK", keyEquivalent: .return) { self.screen = .game }
    }
        .frame(maxWidth: 480)
        .fixedSize()
        .padding()
}

Only the first line of the first `Text` and the first few lines of the second `Text` appear

由于某种原因,我要做的就是将minWidth: 480, idealWidth: 480添加到框架中,并且一切都正确呈现。我没想到这是因为我已经申请了.fixedSize(),所以我认为这三个中的一个应该足够了。

public var body: some View {
    Form {
        Text("Lorem ipsum dolor sit amet, consectetur adipiscing elit.")
            .font(.title)

        Spacer()
            .fixedSize()

        Text("""
            Integer ut orci odio. Proin cursus ut elit eget rutrum. Nunc ante sem, euismod sed purus sed, tempus elementum elit. Phasellus lobortis at arcu quis porta. Cras accumsan leo eu tempus molestie. Suspendisse vulputate diam ipsum, et tristique lorem porta et. Pellentesque sodales est id arcu luctus venenatis.

            Vestibulum non magna lorem. In tincidunt aliquet nunc, sit amet pharetra neque hendrerit id.

            Cras sed!
            """)

        NativeButton("OK", keyEquivalent: .return) { self.screen = .game }
    }
        .frame(minWidth: 480, idealWidth: 480, maxWidth: 480)
        .fixedSize()
        .padding()
}

All the text appears as intended

答案 4 :(得分:2)

看起来font具有换行属性。

如果将其更改为body,则可以正确包装!

enter image description here

答案 5 :(得分:1)

SwiftUI

  

.lineLimit(nil)与.lineLimit(任何数字)

VStack(alignment: .leading, spacing: 16.0) {
// Sets the maximum number of lines that text can occupy in the view.

      Text("SwiftUI is a user interface toolkit that lets us design apps in a declarative way. ")
      .font(.title)
      .lineLimit(3)

// But if you don't know about the the text size then Sets nil in the lineLimit.

      Text("SwiftUI is a user interface toolkit that lets us design apps in a declarative way. That's a fancy way of saying that we tell SwiftUI how we want our UI to look and work, and it figures out how to make that happen as the user interacts with it.... ")
       .font(.body)
       .lineLimit(nil)
}.padding(EdgeInsets(top: 0, leading: 8, bottom: 0, trailing: 8))

答案 6 :(得分:0)

我遇到了一个UIViewRepresentable包装的UITextField的情况,显示为:

VStack {
    Text("Long enough to wrap....")
    Spacer().frame(height: 40)
    CustomTextField()
}

当我添加Text时,CustomTextField停止了换行。我对Text所做的一切都没有帮助。如果我移除了Spacer(),它的包装就很好了!

我结束了移除Spacer并在Text上添加底部填充的问题。据我所知,我的CustomTextField很好,我看不出它为什么会影响SwiftUI布局算法。

答案 7 :(得分:0)

一种方法是使用GeometryReader检查宽度,然后将其应用为框架宽度。很干净。这是一个单独的修饰符,可减少实际布局的混乱。

struct Wrap: ViewModifier {
    func body(content: Content) -> some View {
        GeometryReader { geometry in
            content.frame(width: geometry.size.width)
        }
    }
}

然后在实际的SwiftUI布局中将修饰符应用于文本。

Text("Exports will created to the chosen directory with the optional prefix and/or postfix added to the original filename.")
    .font(.footnote)
    .modifier(Wrap())