Atributika
Atributika copied to clipboard
Support NSTextAttachment in AttributedLabel
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
}
}
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.
Hello :) Are there any news on that bug?
hello, i saw you used gif for nstextattachment, me too. But the gif is not animate, did you solved it?
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.
hi, can you give me the way that you modify library, im stuck in this problem about 4 days
work as expected. thanks
@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.
@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?