CrazyPro007
CrazyPro007

Reputation: 1062

Is there a way that I can limit the text to 2 / 3 lines using SwiftUI?

I am trying using

var body: some View {
    Text("This is large text. Is there a way that I can unwrap the large text as discussed").lineLimit(2)
}

FYI: I knew

var body: some View {
    Text("This is large text. Is there a way that I can unwrap the large text as discussed").lineLimit(nil)
}

It will wrap the text to say n number of lines.

Upvotes: 3

Views: 2445

Answers (2)

mohamed ayed
mohamed ayed

Reputation: 658

because .linelimit is not working as expected for the time being you can simply wrap the Text element inside a VStack if you have multible text like title and subtitme give that VStack multilineTextAlignment modifier and center that text with the parameter .center or you can add .multilineTextAlignment(.center) modifier directly to the Text element :

 VStack{
        Text("This is large text. Is there a way that I can unwrap the large text as discussed")
        .font(.title3)
        .foregroundColor(.white)
                             
        }.padding()
         .multilineTextAlignment(.center)    

                    

Upvotes: 0

NRitH
NRitH

Reputation: 13903

Call .lineLimit(3) on the Text element. (Technically, it can be called on any View, in which case it will limit the lines of all Text elements in that view.)

From SwiftUI.View:

    /// Sets the maximum number of lines that text can occupy in this view.
    ///
    /// The line limit applies to all `Text` instances within this view. For
    /// example, an `HStack` with multiple pieces of text longer than three
    /// lines caps each piece of text to three lines rather than capping the
    /// total number of lines across the `HStack`.
    ///
    /// - Parameter number: The line limit. If `nil`, no line limit applies.
    /// - Returns: A view that limits the number of lines that `Text` instances
    ///   display.
    public func lineLimit(_ number: Int?) -> Self.Modified<_EnvironmentKeyWritingModifier<Int?>>

Upvotes: 3

Related Questions