2

I'm creating a cell which has multiple text and an image in it. I want one text to resize itself and its cell, but how to do it?

I tried using .fixedSize() and some UILabel calculation but I could fix it.

Navigation Button which is also a cell in the list:

NavigationButton(destination: ..., isDetail: true, label: {
                    Cell(model: rowModel)
                })
struct Cell: View {
    var model: OfferModel

    var body: some View {
        VStack {
            HStack {
                Image(systemName: "camera").frame(width: 60, height: 60)
                VStack(alignment: .leading) {
                    Text(model.name)
                        .font(.caption)
                        .textContentType(.name)
                    Text(self.model.text)
                        .lineLimit(3)
                        .multilineTextAlignment(.leading)
                        .font(.title)
                }
                Spacer()
                OfferPriceView(model: model, alignment: .trailing)
            }
            OfferDetailInformationView(key: "Street", value: model.adress, style: .streetAddressLine1)
            OfferDetailInformationView(key: "City", value: model.city, style: .addressCity)
            OfferDetailInformationView(key: "Country", value: model.country, style: .countryName)
        }
    }
}
struct OfferDetailInformationView: View {
    var key: String
    var value: String
    var style: UITextContentType?

    init(key: String, value: String, style: UITextContentType? = nil) {
        self.key = key
        self.value = value
        self.style = style
    }

    var body: some View {
        HStack {
            Text("\(key):").bold()
            Text(value)
            .textContentType(style)
            Spacer()
        }
    }
}
struct OfferPriceView : View {
    let model: OfferModel
    let alignment: HorizontalAlignment

    init(model: OfferModel = .example, alignment: HorizontalAlignment = .center) {
        self.model = model
        self.alignment = alignment
    }

    var body: some View {
        VStack(alignment: alignment) {
            Text("\(String(format: "%.2f", model.cost))\(model.currency)").bold().color(.red)
            Text(model.paymentRate).color(.red)
        }
    }
}

I would like the Text with model.text to be expanded vertically if the text doesn't fit in a single line. For now it just clips the tex in one line.

The view structure is as follows:

  • Content View:
    • List:
      • Navigation Button:
        • Vertical Stack:
          • Horizontal Stack:
            • Image
            • Vertical Stack:
              • Text(model.name)
              • Text(model.text)
            • Spacer
            • Vertical Stack (inside OfferPriceView):
              • Text
              • Text
          • Horizontal Stack (OfferDetailInformationView)
            • Text(currency)
            • Text(payment)
            • Spacer
3
  • post OfferDetailInformationView , OfferPriceView Commented Jun 18, 2019 at 9:29
  • Where is your list? Commented Jun 18, 2019 at 14:55
  • @MojtabaHosseini Edited to add clarification of what is in the list. The Navigation Button is in the list. Commented Jun 20, 2019 at 11:23

4 Answers 4

3

Try

Text("Hello")
.font(.largeTitle)
.minimumScaleFactor(0.3)
Sign up to request clarification or add additional context in comments.

Comments

1

.lineLimit modifier is the one that controls Text height based on their content.

So if you want to have an unlimited lines of text (should be fit in screen or you must use ScrollView), you should use this:

.lineLimit(nil)

UPDATE

Tested in Xcode 11 Beta 7 and nil works as expected to be no limit. But if nil is not acceptable (like it wasn't on earlier versions), so set it to a large integer number:

.lineLimit(Int.max) 

Keep in mind The second option is just a workaround till SwiftUI reaches it first release version.

3 Comments

Oh boy I tried so many options with line limit and it isn’t working, still one line...
Why and how? Maybe it's not the code. Can you share the simple working project?
I've added the View hierarchy, that might help
0

You can use lineLimit(nil) to make text multiline

    Text(repo.description)
                .font(.subheadline)
                .lineLimit(nil)

3 Comments

note that lineLimit(nil) not working atm in the betas
It works, for me on the latest beta, maybe there is some case when it is not working?
on iPhone XS it doesn't expand the lines
0

Tested on Xcode 11.1 GM Seeds, If you don't give any lineLimit modifier to text, then Text will set line limit to infinite as per its text string content.

Generated this:

enter image description here

By Using this Code: enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.