AttributedTextView icon indicating copy to clipboard operation
AttributedTextView copied to clipboard

Laggy scrolling with AttributedTextView inside tableViewCell

Open daurenm opened this issue 6 years ago • 3 comments

Hi there,

Seems like AttributedTextView.attributer.setter takes too much time while blocking main thread.

screen shot 2018-03-31 at 12 49 29 pm

Is there anything we can do to make it faster? Seems like right now, it requires main thread, so I can't simply put in a background thread :(

        let userName = "daurenmuratov"
        let comment = "\(userName):"
            .font(UIFont.systemFont(ofSize: 14, weight: .bold))
            .match(userName)
            .color(.black)
            .makeInteract({ (userLogin) in
                print("Login pressed: \(userLogin)")
            })
            + (" \(text)")
                .font(UIFont.systemFont(ofSize: 14))
                .color(.gray)
                .matchHashtags.color(.red)
                .makeInteract({ (hashtag) in
                    print("Hashtag pressed: \(hashtag)")
                })
                .matchMentions
                .makeInteract({ (mention) in
                    print("Mention pressed: \(mention)")
                })
                .matchLinks
                .makeInteract({ (link) in
                    print("Link pressed \(link)")
                })
                .setLinkColor(.blue)
            textView.attributer = comment

daurenm avatar Mar 31 '18 06:03 daurenm

Hi,

Yes, you ar right, It now assumes you are doing everything on the main. I think that for now you could use a workaround like this:

DispatchQueue.global().async {
    let at: NSMutableAttributedString = "red".red.attributedText
    DispatchQueue.main.async {
        self.myLabel.attributedText = at
    }
}

I will think about how to embed functionality like this into the library.

evermeer avatar Mar 31 '18 07:03 evermeer

A bit late to the party, but don't perform this inside your cell on a background thread unless you are also willing to handle canceling the background task when the cell is dequeued and reused. The better solution would be to have this work done in the UITableViewDatasource so you can process it and then set it on the cell at the proper indexPath if that cell is still being viewed.

jameshays avatar Dec 21 '18 21:12 jameshays

@jameshays Good point. I was already hesitating implementing it the way I suggested.

evermeer avatar Dec 21 '18 22:12 evermeer