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

Kyl*_*man 5 nsstring uilabel ios font-awesome

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

这是我有的:

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

NSString *locationString = [NSString stringWithFormat:@"%@ %@", icon, otherNormalString];
Run Code Online (Sandbox Code Playgroud)

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

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

在此输入图像描述

有关如何正确完成此任务的任何想法?

Rob*_*ier 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
Run Code Online (Sandbox Code Playgroud)

然后,您可以将属性字符串分配给标签的attributedText属性,或者类似地显示它.