使用iOS

时间:2015-08-22 20:39:30

标签: ios objective-c uikit nsattributedstring

我使用NSAttributedString制作UILabel粗体的部分,我一直在研究如何在不同尺寸类别中缩放字体。

我目前看到的唯一方法是在添加属性时使用标签的现有字体大小:

CGFloat fontSize = self.label.font.pointSize;
NSMutableAttributedString * string = [[NSMutableAttributedString alloc] initWithString:@"Abc Abc"];
[string addAttribute:NSFontAttributeName
                   value:[UIFont boldSystemFontOfSize:fontSize]
                   range:NSMakeRange(0,3)];
self.label.attributedText = string;

澄清: label是一个UILabel,带有常规系统字体,属性字符串用于使前3个字母变为粗体。

这种方法运行得相当不错,但将属性字符串的创建与标签相结合。有没有办法在不知道前面的字体大小的情况下缩放属性字符串?

1 个答案:

答案 0 :(得分:1)

在与@Larcerax讨论后,我发现我可以在Interface Builder中的UILabel上使用 Autoshrink 属性来确保字体适当缩小,这样(有点脏)使用可缩小的最大字体大小的解决方案:

NSMutableAttributedString * string = [[NSMutableAttributedString alloc] initWithString:@"Abc Abc"];
[string addAttribute:NSFontAttributeName
               value:[UIFont boldSystemFontOfSize:200.0]
               range:NSMakeRange(0,3)];
self.label.attributedText = string;

但这确实需要提前确定最大尺寸。