Adding Unlimited Lines in a Text (Swiftui)

Adding unlimited lines in a Text (SwiftUI)

For wrapping Text in a Form .lineLimit(Int.max) did not work for me. It seems there needs to be some width for it to know when to wrap. I believe the official way is with .fixedSize:

Text(message)
.fixedSize(horizontal: false, vertical: true)

The documentation states:

This example shows the effect of fixedSize(horizontal:vertical:) on a text view that is wider than its parent, preserving the ideal, untruncated width of the text view.

https://developer.apple.com/documentation/swiftui/view/fixedsize(horizontal:vertical:)

Multiline Text View in SwiftUI

You can set lineLimit to nil for multiple lines.

struct ContentView : View {
var body: some View {
Text("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse est leo, vehicula eu eleifend non, auctor ut arcu")
.lineLimit(nil)
}
}

SwiftUI Text Editor how can I keep track of the number of lines in the editor

I got it to work smoothly and I just needed to make a few changes . With the changes below I just track the size of the Text instead of the number of lines and update the TextEditor accordingly . For the number of lines it only worked when I would press enter .

struct CommentVTestView: View {
@State var Posting = ""
@State var height: CGFloat = 30.0
var body: some View {
VStack(alignment: .leading) {

TextEditor(text: $Posting)
.foregroundColor(.black)
.font(Font.custom("Helvetica Neue", size: 15))
.border(Color.gray)
.padding([.bottom], 5)
.foregroundColor(.secondary)
.cornerRadius(3)
.frame(width: 200, height: height)



Text(Posting)
.foregroundColor(.red)
.frame(width: 190)
.hidden()
.font(Font.custom("Helvetica Neue", size: 15))
.fixedSize(horizontal: false, vertical: true)
.background(GeometryReader { proxy in
Color.clear
.onChange(of: self.Posting, perform: { value in
if proxy.size.height > height {
height = proxy.size.height + 17.0
}
})

})


}
}
}

Text.lineLimit() behavior is inconsistent in SwiftUI

You might also be helped with this answer for the Xcode 11 GM:

https://stackoverflow.com/a/56604599/30602

The summary is that inside other Builders you need to add .fixedSize(horizontal: false, vertical: true) to your Text() to get it to wrap.

SwiftUI Text won't expand into multiple lines

I think this is what you are looking for. Use .fixedSize(horizontal: false, vertical: true) as required.

struct Message: View {
var body: some View {
ZStack() {
Color.blue.cornerRadius(8)
VStack(alignment: .leading, spacing: 8) {
Text("Lorem Ipsum")
.foregroundColor(.white)
.bold()
.font(.system(size: 20))
Text("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Donec enim diam vulputate ut pharetra. Sed turpis tincidunt id aliquet risus feugiat in. Interdum velit laoreet id donec ultrices tincidunt arcu non. Lorem END")
.foregroundColor(.white)
.fixedSize(horizontal: false, vertical: true)
Text("Sent to Group1 by iShaymus")
.foregroundColor(.white)
.italic()
.opacity(0.5)
.font(.system(size: 12))
}
// .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .topLeading)
.padding(12)
}
.fixedSize(horizontal: false, vertical: true)
}
}


Related Topics



Leave a reply



Submit