SwiftUI中UIViewRepresentable的大小

时间:2020-01-14 13:27:34

标签: ios swift swiftui

如何确定我的UIViewRepresentable与父级的宽度相同?我已经为UILabel创建了一个可表示的类,并带有属性文本(从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>) {}

}

用法示例:

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

enter image description here

0 个答案:

没有答案