Open lixiang1994 opened 2 years ago
"UILabel uses an internal NSLayoutManager for laying out the text and drawing it. Unfortunately Apple does not provide a property for us to access or customize it." Source
I use a trick reload attributedText
after the attachment has been loaded:
extension UILabel {
func reloadAttributedText() {
let attr = attributedText
attributedText = nil
attributedText = attr
}
}
Hope it helps!
Hi @Bochbo,
could you please explain a little more about how this reloadAttributedText()
method got called?
I am also facing a situation where the image is downloading but it is not reflecting on the UI.
Hello, @midhunmgopal
Call reloadAttributedText()
after the delegate method textAttachmentDidLoadImage
called.
See the code example below.
```swift class ViewController: UIViewController { private let label = UILabel() override func viewDidLoad() { super.viewDidLoad() view.backgroundColor = .black configureLabel() let imageURL = "https://shorturl.at/GNV01" let attributedString = NSMutableAttributedString(imageURL: imageURL, delegate: self) label.attributedText = attributedString } private func configureLabel() { view.addSubview(label) label.translatesAutoresizingMaskIntoConstraints = false NSLayoutConstraint.activate([ label.topAnchor.constraint(equalTo: view.topAnchor, constant: 16), label.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: -16), label.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -16), label.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 16), ]) } } extension ViewController: AsyncTextAttachmentDelegate { func textAttachmentDidLoadImage(textAttachment: AsyncTextAttachment, displaySizeChanged: Bool) { label.reloadAttributedText() } } extension UILabel { func reloadAttributedText() { let attr = attributedText attributedText = nil attributedText = attr } } ```
Thank you so much @Bochbo ... It worked !!
UILabel:
UITextView: