Atributika icon indicating copy to clipboard operation
Atributika copied to clipboard

Support NSTextAttachment in AttributedLabel

Open denniskeithgaso opened this issue 5 years ago • 8 comments

Hello! This library is very excellent. I am using NSMutableAttributedString to add NSTextAttachment based on the example you gave in https://github.com/psharanda/Atributika/issues/33

It was working well, however when I added onClick on my Attributed label, it cannot detect tag attributes. I think this is because the NSMutableAttributedString doesn't have the detections from the AttributedText.

I have the following code: It inserts a custom emoji. I wanted to detect a tag with class mentioned-user This is a mentioned user but I don't want to use the built-in mentions detection because I want to get the user id from the tag. plainView.message is the AttributedLabel Any help is very much appriciated.

//this is the Style I used for my custom mention tag
let span = Style("mentioned").foregroundColor(UIColor.CustomColor.cryptoYellow, .normal)
                .foregroundColor(.blue, .highlighted)

let str = messageTxt.style(tags: tags, transformers: transformers)
let mutableAttrStr = NSMutableAttributedString(attributedString: str.attributedString)

var locationShift = 0
for detection in str.detections {
    switch detection.type {
    case .tag(let tag):
        if let emojiColon =  tag.attributes["colon"] {
            let textAttachment = NSTextAttachment()
            let custom = CustomEmoji.customEmojis[emojiColon]!
            if custom.ext == "gif" {
                if let data = custom.data {
                        let image = UIImage.gif(data: data)
                            textAttachment.image = image
                } else {
                    Alamofire.request(custom.imageUrl).responseImage { (response) in
                        if let image = response.result.value {
                            let image = UIImage.gif(data: response.data!)
                            DispatchQueue.main.async {
                                textAttachment.image = image
                            }
                        }
                    }
                }
            } else {
                textAttachment.image = custom.image
            }
            textAttachment.setImageHeight(20)
            let imageAttrStr = NSAttributedString(attachment: textAttachment)
            let nsrange = NSRange.init(detection.range, in: mutableAttrStr.string)
            mutableAttrStr.insert(imageAttrStr, at: nsrange.location + locationShift)
            locationShift += 1
        }
    default:
        break
    }
}

plainView.message.attributedText = mutableAttrStr
    .styleLinks(link)
    .styleAll(all)
plainView.message.onClick = { label, detection in
    switch detection.type {
    case .tag(let tag):
        let t = tag.attributes
        if let spanClass = t["class"] {
            if spanClass == "mentioned-user" {
                let id = t["data-userid"] ?? ""
                let user_name = t["data-uname"] ?? ""
                self.selectDelegate?.selectedUserId(id)
            }
        } else if let href = t["href"] {
            if let url = URL(string: href) {
                UIApplication.shared.open(url, options: [:], completionHandler: nil)
            }
        } else if let type = t["type"] {
            if type == "channel" {
                if let id = t["id"] {
                    self.selectDelegate?.selectedConvo(id)
                }
            }
        }
        default:
        break
    }
}

denniskeithgaso avatar Oct 04 '18 09:10 denniskeithgaso

Hi, thanks for your interest in Atributika. It's true that NSTextAttachment is not working with AttributedLabel for the moment. I will try find some time to add support of it.

psharanda avatar Oct 18 '18 10:10 psharanda

Hello :) Are there any news on that bug?

djavid avatar Apr 17 '20 11:04 djavid

hello, i saw you used gif for nstextattachment, me too. But the gif is not animate, did you solved it?

fukemy avatar Jun 15 '20 08:06 fukemy

hello, i saw you used gif for nstextattachment, me too. But the gif is not animate, did you solved it?

I solved it by modifying the library a little bit. I inserted animated UIImageView in between text. I did not use NSTextAttachment. Just an animated UIImageView.

denniskeithgaso avatar Jun 15 '20 09:06 denniskeithgaso

hi, can you give me the way that you modify library, im stuck in this problem about 4 days

fukemy avatar Jun 15 '20 09:06 fukemy

work as expected. thanks

fukemy avatar Jun 17 '20 04:06 fukemy

@denniskeithgaso, hello! Hope you are doing well.

Can you please tell me what's a workaround you found for this issue:

I think this is because the NSMutableAttributedString doesn't have the detections from the AttributedText.

Or how to overcome NSTextAttachment and use any kind of ImageVIew there.

dmytrostriletskyi avatar Oct 06 '20 19:10 dmytrostriletskyi

@denniskeithgaso I just need to show the image and do not need the tap. In that case, your code should download the image and show it but I was wondering does it ever show the image. Because when the attributed string is built the image is not present, it is being downloaded, and after that textAttachment's image is set. By this time the attributed string is already shown. Setting the image on textAttachment after that has no effect in my case. @psharanda How do we update the textAttachment after the image is downloaded?

tarun-personatech avatar May 23 '23 13:05 tarun-personatech