Adding unlimited lines in a Text (SwiftUI)

后端 未结 5 562
轮回少年
轮回少年 2020-12-06 04:53

Just figuring out how I can achieve multiple lines of text in a Text. It seems like the Text has the same default as UILabel (one line

相关标签:
5条回答
  • 2020-12-06 05:29

    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.

    0 讨论(0)
  • 2020-12-06 05:36

    If lineLimit(nil) is not working for you, try setting layoutPriority manually to whatever it suits you .layoutPriority(0.5)

    0 讨论(0)
  • 2020-12-06 05:40

    My text kept truncating even with no line limit applied. Wrapping my content in a ScrollView {} solved it.

    See more info here: https://www.hackingwithswift.com/quick-start/swiftui/how-to-add-horizontal-and-vertical-scrolling-using-scrollview

    0 讨论(0)
  • 2020-12-06 05:44

    Use .lineLimit() to limit the amount of lines of text. It takes an optional Int (Int?) as an argument, and .lineLimit(nil) allows unlimited lines.

    Edit: As of SwiftUI Beta 5, Text has a default line limit of nil, so text in Text will wrap by default.

    0 讨论(0)
  • 2020-12-06 05:46

    Use this:

    Text("Ingredients: Avocado, Almond Butter, Bread")
        .lineLimit(nil)
    

    If that doesn't work, use this method:

    Text("Ingredients: Avocado, Almond Butter, Bread")
        .fixedSize(horizontal: false, vertical: true)
    
    0 讨论(0)
提交回复
热议问题