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:
Any idea on how I can properly accomplish this?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With