Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AsyncTextAttachment并不能很好的支持UILabel, 将Demo中的UITextView替换成UILabel, 无法显示图片, #142

Open
lixiang1994 opened this issue Apr 24, 2022 · 5 comments

Comments

@lixiang1994
Copy link

lixiang1994 commented Apr 24, 2022

UILabel:
label

UITextView:
textview

@lixiang1994
Copy link
Author

WX20220424-170717@2x

@bohdan-ios
Copy link

"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!

@midhunmgopal
Copy link

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.

@bohdan-ios
Copy link

Hello, @midhunmgopal

Call reloadAttributedText() after the delegate method textAttachmentDidLoadImage called.
See the code example below.

Code example

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
    }
}

@midhunmgopal
Copy link

Thank you so much @Bochbo ...
It worked !!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants