Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS FontAwesome in middle of another string

I am trying to display some text and an icon inside a single string using FontAwesome.

Here is what I have:

NSString *icon = [NSString fontAwesomeIconStringForIconIdentifier:@"icon-map-marker"];

NSString *locationString = [NSString stringWithFormat:@"%@ %@", icon, otherNormalString];

Basically I want to have the map marker show up in front of the location being displayed. Using FontAwesome should make this really simple but I can't quite get it to work right.

Here is also what shows up if I NSLog the string icon:

enter image description here

Any idea on how I can properly accomplish this?

like image 693
Kyle Begeman Avatar asked Jan 03 '14 00:01

Kyle Begeman


1 Answers

Strings do not carry any style information (including font). In order to include font information, you'll need to make an NSAttributedString. All this -fontAwesomeIconStringForIconIdentifier method is doing is returning a unicode value given an identifier.

On the assumption that you're using ios-fontawesome, you'd want to do something like:

... your locationString assignment ...
NSMutableAttributedString *astring = [[NSMutableAttributedString alloc] initWithString:locationString];

[astring addAttribute:NSFontAttributeName 
                value:[UIFont iconicFontOfSize:size] 
                range:NSMakeRange(0,1)]; // The first character

You can then assign the attributed string to a label's attributedText property, or similarly display it.

like image 173
Rob Napier Avatar answered Oct 08 '22 11:10

Rob Napier