How to Limit Number of Lines for Text in SwiftUI?

SwiftUI Text – Limit Number of Lines

To limit number of lines for text in Text view, call lineLimit() method on this Text view, with specific number of lines value passed as argument.

Example

In the following example, we have a Text view. We will set its line limit to 2, using lineLimit() method.

ContentView.swift

import SwiftUI

struct ContentView: View {
    var body: some View {
        Text("Hello World! This is a sample text to test line limit. And this is some more text for testing.")
            .font(.title)
            .lineLimit(2)
    }
}

Screenshot

SwiftUI Text - Limit Number of Lines

Line Limit = 1

The following would be the result, if the line limit is 1.

ContentView.swift

import SwiftUI

struct ContentView: View {
    var body: some View {
        Text("Hello World! This is a sample text to test line limit. And this is some more text for testing.")
            .font(.title)
            .lineLimit(1)
    }
}

Screenshot

SwiftUI Text - Limit Number of Lines

Summary

Summarising this SwiftUI Tutorial, we learned how to limit number of lines for the text in Text view.