TestsTested | ✗ |
LangLanguage | Obj-CObjective C |
License | MIT |
ReleasedLast Release | May 2015 |
Maintained by Matt Styles.
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.
KILabel doesn't have any special dependencies so just include the files from KILabel/Source in your project. Then use the KILabel class in place of UILabel.
As Podspec is also provided so KILabel can be used by added the following line to your project's Podfile.
pod 'KILabel', '1.0.0'
You can also use KILabel with Swift. Just compile the files into your XCode project in the usual way but add the following line to your Objective-C Bridging Header.
#import "KILabel.h"
The code snippet below shows how to setup a label with a tap handling block. A more complete example can be seen in the KILabelDemo project included in the repository.
// Create the label, you can do this in Interface Builder as well
KILabel *label = [[KILabel alloc] initWithFrame:NSRectMake(20, 64, 280, 60)];
label.text = @"Follow @krelborn or visit http://compiledcreations.com #shamelessplug";
// Attach a block to be called when the user taps a user handle
label.userHandleLinkTapHandler = ^(KILabel *label, NSString *string, NSRange range) {
NSLog(@"User tapped %@", string);
};
// Attach a block to be called when the user taps a hashtag
label.hashtagLinkTapHandler = ^(KILabel *label, NSString *string, NSRange range) {
NSLog(@"Hashtag tapped %@", string);
};
// Attach a block to be called when the user taps a URL
label.urlLinkTapHandler = ^(KILabel *label, NSString *string, NSRange range) {
NSLog(@"URL tapped %@", string);
};
[self.view addSubview:label];
KILabel also works in Swift. Here's the above example again but in swift.
// Create the label, you can do this in Interface Builder as well
let label = KILabel(frame: CGRect(x: 20, y: 64, width: 280, height: 60))
label.text = "Follow @krelborn or visit http://compiledcreations.com #shamelessplug"
// Attach a block to be called when the user taps a user handle
label.userHandleLinkTapHandler = { label, handle, range in
NSLog("User handle \(handle) tapped")
}
// Attach a block to be called when the user taps a hashtag
label.hashtagLinkTapHandler = { label, hashtag, range in
NSLog("Hashtah \(hashtag) tapped")
}
// Attach a block to be called when the user taps a URL
label.urlLinkTapHandler = { label, url, range in
NSLog("URL \(url) tapped")
}
view.addSubview(label)
The repository includes KILabelDemo, written in Objective-C, that shows a simple use of the label in a storyboard with examples for implementing touchable links.
The demo also demonstrates how to use a gesture recognizer with the label to implement a long press on a link, which uses the linkAtPoint method.
There's also an example using a UITableView where cells are given dynamic heights depending on the content.
KILabel is available under the MIT license.
KILabel was inspired by STTweetLabel (http://github.com/SebastienThiebaud) and others such as NimbusAttributedLabel (http://latest.docs.nimbuskit.info/NimbusAttributedLabel.html). If KILabel can't help you, maybe they can.
Open an issue to report bugs or request a feature.
Any otherfeedback welcome through the obvious channels.