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

Support NSTextAttachment in AttributedLabel #54

Open
denniskeithgaso opened this issue Oct 4, 2018 · 8 comments
Open

Support NSTextAttachment in AttributedLabel #54

denniskeithgaso opened this issue Oct 4, 2018 · 8 comments
Assignees

Comments

@denniskeithgaso
Copy link

denniskeithgaso commented Oct 4, 2018

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
    }
}
@psharanda
Copy link
Owner

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 psharanda self-assigned this Oct 18, 2018
@psharanda psharanda added the bug label Oct 18, 2018
@psharanda psharanda added enhancement and removed bug labels Nov 25, 2018
@psharanda psharanda changed the title .style(tags: [], transformers: transformers) support for NSMutableAttributedString Support NSTextAttachment in AttributedLabel Jan 23, 2019
@djavid
Copy link

djavid commented Apr 17, 2020

Hello :)
Are there any news on that bug?

@fukemy
Copy link

fukemy commented Jun 15, 2020

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

@denniskeithgaso
Copy link
Author

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.

@fukemy
Copy link

fukemy commented Jun 15, 2020

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

@fukemy
Copy link

fukemy commented Jun 17, 2020

work as expected. thanks

@dmytrostriletskyi
Copy link

dmytrostriletskyi commented Oct 6, 2020

@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.

@tarun-personatech
Copy link

tarun-personatech commented May 23, 2023

@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?

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

No branches or pull requests

6 participants