在 iOS 中使用大小 类 缩放 NSAttributedString

Scaling NSAttributedString with Size Classes in iOS

我正在使用 NSAttributedStringUILabel 的一部分设为粗体,并且我一直在研究如何将字体缩放到不同大小 类。

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

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个字母加粗。

这工作得相当好,但将属性字符串的创建与标签结合在一起。有没有办法在不知道字体大小的情况下缩放属性字符串?

在与@Larcerax 讨论后,我发现我可以在 Interface Builder 的 UILabel 上使用 A​​utoshrink 属性 来确保字体缩小适当地,它给出了使用可以缩小的可憎大最大字体大小的(有点脏)解决方案:

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

不过,这确实需要提前确定最大尺寸。