5

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))
    }
}
}

ˋˋˋ

Adrian Baumgart
  • 69
  • 1
  • 1
  • 7
  • 1
    Have you done *any* searching on this on this site? Could you post what code you've tried? A very quick search on *[SwiftUI] multiple line" turns up no less than two answers, along with a dup. Maybe you could explain why they haven't worked for you? (And sure, if it's a different issue I'm apologizing in advance, but still asking for your code so maybe we could help you.) –  Jun 27 '19 at 21:19
  • You might be helped with this answer: https://stackoverflow.com/a/56604599/30602 – Wil Shipley Oct 11 '19 at 22:34

1 Answers1

2

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)
        }
    }
}
Md Shafiul Islam
  • 1,579
  • 1
  • 13
  • 19