I've released an update to KILabel to Github that brings the code to an official 1.0 version. KILabel is a replacement for UILabel that allows links to be embedded in the displayed text. It highlights Twitter style usernames, hashtags and URLs and allows the developer to use blocks to respond to taps on them. The design attempts to copy the look and feel of Tweet labels in the official Twitter iOS app and should be useful to anyone wanting to present Tweets or other messages in a way that is familiar to users. I originally wrote about it on my old personal blog Embed links in a UILabel - MatthewStyles.com which has more background on how it works.

The code has remained reasonably static over the last 12 months but I wanted to clean things up a bit and update the example code to address some of the issues people have had. Here's a quick summary of the changes...

  • Added Swift examples to documentation
  • Added example of using Auto Layout to automatically calculate table row heights
  • Added CocoaPods integration
  • Improved Swift support by adding Nullability annotations to Objective-C implementation
  • Improved naming of tap handler members
  • Improved so KILabel now draws inside Interface Builder, exposes some properties through the attributes inspector
  • Improved so KILinkType is now an enum, added KILinkTypeOption to specify combinations of types
  • Minor changes to improve code readability
  • Updated example project to 8.3 SDK and replaced deprecated classes

The changes mean that your existing project will probably not compile with the 1.0.0 version. Making it work should just be a case of correcting the names of the tap handlers.

CocoaPods

The only feature request I've had has been to add support for CocoaPods. This is now done and the Podspec has been submitted to the repository. You just need to add the following to your project's Podfile to get version 1.0.0 into your project:-

pod 'KILabel', '1.0.0'

The great thing about this is that the component can now be found through the CocoaPods search engine and even better we have API documentation published to CocoaDocs.org which looks great and has made all those Doc comments worthwhile.

Dynamic UITableViewCell Heights with Auto Layout

I've had a few questions about how to correctly get a KILabel to resize when in a UITableView. I've previously given example code that uses a dummy KILabel to measure the text in order to specify the height through tableView:heightForRowAtIndexPath:. Given that we're now in a world of Auto Layout that really shouldn't be necessary. After much head scratching and some serious cursing I found a nice solution which I've included in the demo.

The solution is thanks to the article Dynamic Table View Cell Height and Auto Layout on RayWenderlich.com. The trick is to first set a full set of constraints around the the label, then set its Content Compression Resistance and Content Hugging Priorities to high. The priorities want to be above 750 so they take precedence over the cell's content view. You're mileage will vary depending on how you want different elements to resize. Once the constraints are setup you want you need to set the estimatedRowHeight property of the UITableView to a non-zero value in order to trigger the automatic row height calculations to kick in.

This solution may not be the most efficient but it requires very little code. In fact you can probably remove a bunch of code which is no longer needed, which is always a good thing.

Official Swift Support

After a slow start I've become a big fan of Swift. XCode 6.3 introduced changes to improve integration between Swift and Objective-C code with nullability annotations. I've added these to the code so previously implicitly unwrapped optionals are replaced with the appropriate types. NSHipster has a good summary on nullability annotations and other recent changes to Swift here.

Summary

That's a brief overview of the recent changes. The README on the Github page has up to date information on how to use the class as well as the example code in the repository.

So what do think? If you find KILabel useful or are already using it in a project? Let me know in the comments.

 

Share this: