5

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?

Marcus Leon
  • 53,069
  • 115
  • 287
  • 420
Kyle Begeman
  • 7,139
  • 9
  • 39
  • 57

1 Answers1

5

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.

Rob Napier
  • 266,633
  • 34
  • 431
  • 565
  • This answer is correct, should be flagged as so.. Perhaps you can include a working example so copying/pasting works out of the box. – alasker Jun 29 '15 at 16:17