-
Notifications
You must be signed in to change notification settings - Fork 158
Open
Labels
Description
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
}
}