Open soulfoodz opened 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...
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.
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.
Any thoughts? I'm leaning towards the second solution. Its more complex but it would be cleaner in the long run.
I like the custom string matches mentioned in the second solution. Thanks for being so responsive!
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.
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'
This is now creeping towards release. Merged changes into main development branch.
pod 'KILabel', :branch: 'develop'
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.
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.
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
any resolution on this?
Instagram style support where a tappable username doesn't have an @ sign in front of it.