I'm using NSAttributedString
to make a section of a UILabel
bold, and I've been looking at how I might scale the font across different size classes.
The only way I can currently see to do this is to use the existing font size of the label when adding the attribute:
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;
To clarify: label
is a UILabel
with a regular system font, and the attributed string is used to make the first 3 letters bold.
This works reasonably well, but couples the creation of the attributed string with the label. Is there a way to scale the attributed string without knowing the font size up front?
After discussion with @Larcerax, I found that I could use the Autoshrink property on my UILabel
in Interface Builder to ensure the font scales down appropriately, which gives the (somewhat dirty) solution of using an obscenely large maximum font size which can be scaled down:
NSMutableAttributedString * string = [[NSMutableAttributedString alloc] initWithString:@"Abc Abc"];
[string addAttribute:NSFontAttributeName
value:[UIFont boldSystemFontOfSize:200.0]
range:NSMakeRange(0,3)];
self.label.attributedText = string;
This does require deciding upon a maximum size ahead of time, though.