psharanda / Atributika

Convert text with HTML tags, links, hashtags, mentions into NSAttributedString. Make them clickable with UILabel drop-in replacement.
MIT License
1.45k stars 155 forks source link

Emoji Support #125

Closed qxtaiba closed 4 years ago

qxtaiba commented 4 years ago

I'm currently working with Atributika and my use-case specifically applies to the hashtag functionality. I have noticed that the parser doesn't seem to include emojis in the tag and I was wondering if any of you have a fix/workaround for this?

To just give you all an example, let's say the hashtag I'm working with is #Atributika❤️, but the parser will only parse up to #Atributika and ignores the emoji.

let font = Style.font(UIFont.init(name: "OpenSans", size: 12)!).foregroundColor(UIColor.lightGray)

let contentStyling = Style("a")
    .foregroundColor(UIColor.lightGray, .normal)
    .foregroundColor(UIColor.white, .highlighted)

label.lineBreakMode = .byWordWrapping
label.numberOfLines = 0
label.attributedText = #Atributika❤️
    .style(tags: contentStyling)
    .styleHashtags(contentStyling)
    .styleMentions(contentStyling)
    .styleLinks(contentStyling)
    .styleAll(font)

label.frame = CGRect(x: 185, y: 140, width: 165, height: 90)

label.onClick = { label, detection in
            switch detection.type {
            case .hashtag(let tag):
                var passTag = ["tag":"#\(tag)"]
                self.tapTag = true
                self.dismiss(animated: true, completion: nil)
                SVProgressHUD.show(withStatus: "Loading projects")
                NotificationCenter.default.post(name: NSNotification.Name(rawValue: "NotificationID"), object: nil, userInfo: passTag as [AnyHashable : Any])

            case .mention( _): break
            case .link( _): break
            case .tag( _): break
            default: break
            }
        }

view.addSubview(label)
psharanda commented 4 years ago

Hi, Thanks for your feedback. Yeah it definitely makes sense to support emojis inside hashtags and mentions. Fix is already released in v4.9.9 Enjoy :)

qxtaiba commented 4 years ago

Hey! I just tried running your latest commit, although it still isn't recognizing hashtags with emojis as hashtags. I tested this by adding a breakpoint and when there is an emoji at the end, the hashtag case is never triggered.

qxtaiba commented 4 years ago

I tried creating a PR with a fix that worked for me but I don't have access to this repository.

If you simply replace return detect(regex: "[#]\\w\\S*\\b") with return detect(regex: "[#]\\S+") then you'll find that emojis are detected as part of the tag.

psharanda commented 4 years ago

Hm, you likely not running latest commit (v4.9.9). In latest version regex was modified as follows:

    public func detectHashTags() -> [Range<String.Index>] {

        return detect(regex: "#[^[:punct:][:space:]]+")
    }

    public func detectMentions() -> [Range<String.Index>] {

        return detect(regex: "@[^[:punct:][:space:]]+")
    }
psharanda commented 4 years ago

FYI you don't need access to repo to create PR. Check this guide https://www.freecodecamp.org/news/how-to-make-your-first-pull-request-on-github-3/