1

How do I make sure my UIViewRepresentable has the same width as it's parent? I have created a representable class for a UILabel with an attributed text (converted from HTML).

AttributedLabel.swift:

struct AttributedLabel: UIViewRepresentable {

    let attributedText: NSAttributedString

    func makeUIView(context: UIViewRepresentableContext<Self>) -> UILabel {
        let label = UILabel()
        label.attributedText = attributedText
        label.lineBreakMode = .byWordWrapping
        label.numberOfLines = 0
        label.backgroundColor = UIColor.red

        return label
    }

    func updateUIView(_ uiView: UILabel, context: UIViewRepresentableContext<Self>) {}

}

Usage example:

List {
    VStack {
        AttributedLabel(attributedText: attributedText)
    }
}

enter image description here

Chris
  • 282
  • 2
  • 9
  • The question is incorrect. Containers in SwiftUI do not have own size, until you specify it explicitly with `.frame`, and forward size calculations to content, ie. children, ie in your case to `AttributedLabel`. – Asperi Jan 14 '20 at 14:36
  • OK, then how do I make the contained view respect the bounds of the parent view? In autolayout I would constrain the left and right side of the view to the parent to make sure it doesn't grow beyond the width of the parent. – Chris Jan 14 '20 at 14:54

0 Answers0