I am trying to choose a "system font" for my labels in Interface Builder. All of these labels contain an attribute string. If they were “pain shortcuts”, I could just choose “system font” and my application would automatically use “Helvetica” for iOS 8 and “San Francisco” for iOS 9 +.
The problem is that this is not possible when using attributed strings. (I need them for the space between lines).
Even if I had to download fonts and select one in the interface builder, it will not automatically change depending on iOS, as the labels of "plain text" do.
Is there any way to do this? Maybe I missed something.
By the way, my current job is to connect all the labels to the output array and skip them by creating an NSMutableAttributedString, and then just "overriding" the font property with a programmatically obtained system font.
The disadvantage of this is that tags are created twice, so they are slower on screens with a lot of tags.
Edit: The code I use to “change” the font:
override func viewDidLoad() {
super.viewDidLoad()
for label in labelsToFormat {
let targetFont = UIFont.systemFont(ofSize: label.font.pointSize)
let originalLabel = NSMutableAttributedString(attributedString: label.attributedText!)
originalLabel.addAttribute(NSFontAttributeName, value: targetFont, range: NSMakeRange(0, originalLabel.length))
label.attributedText = originalLabel
}
for label in labelsToFormatBold {
let targetFont = UIFont.boldSystemFont(ofSize: label.font.pointSize)
let originalLabel = NSMutableAttributedString(attributedString: label.attributedText!)
originalLabel.addAttribute(NSFontAttributeName, value: targetFont, range: NSMakeRange(0, originalLabel.length))
label.attributedText = originalLabel
}
}
source
share