News.me

Developing Stories: Custom Label iOS Library

Hello out there news fans!

At a recent Brooklyn iOS Dev Meetup, I talked about our use of Core Text in the News.me iPhone app. In building out the app, I created a subclass of UILabel that takes advantage of Core Text for more robust formatting, such as kerning, mixed fonts, & line height. I thought this might be helpful to other developers out there, so we open-sourced it, and it’s now available on Github.com.

You can use the class much like you would UILabel. I’ve only included the features that we initially needed, but as people use it, it should be pretty straightforward to extend it to incorporate other Core Text features without too much effort.

Here’s a example of using the class:

NMCustomLabel *label = [[NMCustomLabel alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
label.text = @"Tacos are <b>delicious</b>, <i>seriously</i>";
label.font = [UIFont fontWithName:@"HelveticaNeue" size:12];
label.fontBold = [UIFont fontWithName:@"HelveticaNeue-Bold" size:13];
label.fontItalic = [UIFont fontWithName:@"HelveticaNeue-LightItalic" size:12];
label.kern = -0.5;
label.textColor = [UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:1.0];
label.textColorBold = [UIColor colorWithRed:53/255.0 green:53/255.0 blue:53/255.0 alpha:1.0];
label.lineHeight = 12;
[self.view addSubview:label];

Check out the project & let me know what you think – I’m @tolar on Twitter.

-Rob Haining
iOS Developer at News.me