Line Break with Text() in SwiftUI

Viewed 14930

How do I make a line break with Text() if the text is too long to display in one line? (Something like lineBreakMode in UIKit)

If I type in a long string it just adds „...“ on the right side of the text.

Thanks!

Edit: I tried to combine it with a scroll view to be able to scroll if the text is too long, but if I add a scroll view it ignors .lineLimit()

Code:

ˋˋˋ

struct Homework: View {

var selectedWeek: String
var week: String = "10.-20."
var content: String = "" //Long string inside here

var body: some View {
    NavigationView {
        ScrollView(isScrollEnabled: true, alwaysBounceHorizontal: false, alwaysBounceVertical: true, showsHorizontalIndicator: false, showsVerticalIndicator: true, content: {
            Text(content)
               .lineLimit(nil)
        })
            .navigationBarTitle(Text(week))
    }
}
}

ˋˋˋ

1 Answers

lineLimit API is used whenever you want a specific number of lines in the Text, you just call this API with number of lines that you want. If you are not sure how many lines will the take, just give nil in the parameter.

import SwiftUI
struct ContentView : View {
    @State var demoText = "Start Typing"
    var body: some View {
        VStack {
            TextField($demoText)
            Text(demoText)
                .lineLimit(nil)
        }
    }
}
Related