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

Click Detection Type Hashtag #87

Closed TomKremer closed 5 years ago

TomKremer commented 5 years ago

Hello,

first let me thank you for your amazing work! It's saving me a ton of work!

I got an issue with hashtag detection type with the following use-case: <a href="somedomain.tld/path/etc">#NoTwitterHashtag</a>

Your code detects a hashtag, which works fine but I think if a href is available, it should be considered as a normal link type?

Best, Tom.

futuretap commented 5 years ago

Yes, it would be great if a href links would be converted to NSAttributedString.Key.link. Then I could use the attributed string output in a standard UITextView, too.

psharanda commented 5 years ago

@futuretap You can use tuner for this use case


let a = Style("a")

let test = "<a href=\"https://github.com/psharanda/Atributika\">link</a>".style(tags: a, tuner: { style, tag in
    if tag.name == a.name {
        if let link = tag.attributes["href"] {
            return style.link(URL(string: link)!)
        }
    }
    return style
}).attributedString
psharanda commented 5 years ago

@TomKremer Unfortunately your use case is out of scope, because Atributika is designed to know nothing about tags meaning.

I would suggest to just hardcode required logic in your app. In click handler of AttributedLabel you get detection, then check if it is overlapped with detection with higher priority and handle it in case of that (so prefer href detection over hashtag one)

TomKremer commented 5 years ago

Many thanks for your feedback, will try your suggestion!

markst commented 2 years ago

Came across this issue having implemented my own tuner for transforming href into link style. Does seem strange to me this isn't implicit.

Use case is for handling NSAttributedString.Key.link in a UITextView without using AttributedLabel. Do wonder if it might be worth including an example tuner in README.