Atributika icon indicating copy to clipboard operation
Atributika copied to clipboard

AttributedLabel height not set in SwiftUI List

Open dannyBuonocore opened this issue 5 years ago • 3 comments

Maybe I'm doing something wrong, but I'm having issues setting the height of a multi-line AttributedLabel when it's in a SwiftUI List or ScrollView.

Please refer to this minimal reproducible example

It works perfectly fine when I use a UITextView or a UILabel but I need to handle touches on html links.

dannyBuonocore avatar Oct 15 '20 20:10 dannyBuonocore

Hi, Works for me if set translatesAutoresizingMaskIntoConstraints to false

struct HTMLViewAttributedLabel: UIViewRepresentable {
  let html: String
  
  func makeUIView(context: Context) -> AttributedLabel {
    let all = Style.font(.systemFont(ofSize: 16))
    let link = Style("a")
      .foregroundColor(.systemTeal, .normal)
      .foregroundColor(.systemTeal, .highlighted)
    
    let attributedText = html
      .style(tags: link)
      .styleLinks(link)
      .styleAll(all)
    
    let label = AttributedLabel()
    label.attributedText = attributedText
    label.numberOfLines = 0
    label.translatesAutoresizingMaskIntoConstraints = false
    return label
  }
  
  func updateUIView(_ uiView: AttributedLabel, context: Context) {
    
  }
}

psharanda avatar Oct 25 '20 12:10 psharanda

any update?

sonysm avatar Feb 02 '22 08:02 sonysm

I defined a variable on swiftUI side @State var size:CGSize = .zero and I binded size to this view self.size = label.sizeThatFits(label.superview?.bounds.size ?? .zero) and I can use size like this : .frame(width: size.width , height: size.height)

bedirhanyesilbas avatar Feb 19 '22 12:02 bedirhanyesilbas

Please check a SwiftUI example in the demo project

psharanda avatar Jun 04 '23 10:06 psharanda