如何确定我的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)
}
}