iOS FontAwesome位于另一个字符串的中间

时间:2014-01-03 00:56:22

标签: ios nsstring uilabel font-awesome

我正在尝试使用FontAwesome在单个字符串中显示一些文字和图标。

这就是我所拥有的:

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

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

基本上我想让地图标记出现在显示的位置前面。使用FontAwesome应该让这很简单,但我不能让它正常工作。

如果我NSLog字符串图标,这也是显示的内容:

enter image description here

关于如何正确实现这一点的任何想法?

1 个答案:

答案 0 :(得分:5)

字符串不带任何样式信息(包括字体)。要包含字体信息,您需要制作NSAttributedString。所有这个-fontAwesomeIconStringForIconIdentifier方法正在做的是返回给定标识符的unicode值。

假设您正在使用ios-fontawesome,您需要执行以下操作:

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

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

然后,您可以将属性字符串分配给标签的attributedText属性,或者同样显示它。