Krelborn / KILabel

A simple to use drop in replacement for UILabel for iOS 7 and above that highlights links such as URLs, twitter style usernames and hashtags and makes them touchable.
MIT License
470 stars 132 forks source link

Add tappable "hot" items without a visible @ or # symbol #16

Open soulfoodz opened 9 years ago

soulfoodz commented 9 years ago

Instagram style support where a tappable username doesn't have an @ sign in front of it.

Krelborn commented 9 years ago

@soulfoodz I'm wondering if what is really needed is to implemented issue #1 and allow any sequence of characters in the label to become a link.

There's 2 possible ways to allow this...

1. Custom Link Ranges

Add a method to the KILabel class like addLinkForRange:. This will add the given range to internal link ranges collection. Problem is this would have to be done anytime the text changed and would be completely up to the caller to manage the links. There would need to be a removeLinkForRange call as well, which could get a bit messy.

2. Custom Link Detectors

An alternative to the above would be to allow rules to be defined for detecting links. Rules could be added to a label instance at construction or when changing text depending on requirements. Rules would really just be a block that would return the ranges of links it detects and really simple rules could be provided out of the box to detect based on string matches, regex and ranges.

The advantage to this method over the first is that rules would automatically be re-evaluated whenever the text changed and of coarse any rules you write can be reused.

Summary

Any thoughts? I'm leaning towards the second solution. Its more complex but it would be cleaner in the long run.

soulfoodz commented 9 years ago

I like the custom string matches mentioned in the second solution. Thanks for being so responsive!

Krelborn commented 9 years ago

I think I've come up with a nice solution now.

Custom link types are now defined using instances of a new KILabelLinkClassifier class. You provide a block that does the link parsing and provides the ranges. This class also manages the link's tap handler block and attributes so you will have as much control of custom links as the default set. Links can be usernames, email addresses, dates, phone number, anything you like.

The current link types still exist, but internally will be implemented in terms of classifiers. The public interface will not change with respect to these.

It seems to be shaping up quite nicely but needs a bit more testing to make sure nothing's broken. Should have something to push to the dev branch in couple of days.

Krelborn commented 9 years ago

First stab at implementing this is now up on its own branch. Probably still needs a bit of work to clean up names of stuff and test but its not far off.

pod 'KILabel', :branch: 'feature/llink-classifiers'
Krelborn commented 9 years ago

This is now creeping towards release. Merged changes into main development branch.

pod 'KILabel', :branch: 'develop'
d6u commented 9 years ago

I think both approach should works fine, each people will have different needs. What I did is much simpler. I removed all auto link detection, just use a custom attribute and do link detection just for those attributes.

https://github.com/d6u/TapLabel/blob/9ed680d212b94b4b3df5ff888a95ec664196721a/Source/TapLabel.swift#L108

Krelborn commented 9 years ago

Yep everyone has different needs. Hopefully this solution is flexible enough for the majority of use cases, most links can be detected with a bit of simple regex. To be honest, the whole point of the class is to do the auto link detection, it came about because of my frustration that I could do it with a text field but not a label.

AdityaGuiddit commented 8 years ago

I have dowloaded the updated version and have fair bit of understanding of the KILabelLinkClassifier class, but i am not able to have the hot @ or # without actually having to display them. The KILabel only detects them if @ or # are included.

Can you please elaborate on how exactly do i Add tappable "hot" items without a visible @ or # symbol

Steven4294 commented 8 years ago

any resolution on this?