I have a UILabel, whose text
or attributedText
properties I update programatically. Under some cases, I'd like to italicize it. I could either:
A) set the UILabel.font
property:
myLabel.font = [UIFont italicSystemFontOfSize: 12.0];
B) or use an attributed string to do something similar:
myLabel.attributedText = [[NSAttributedString alloc] initWithString: @"my text" attributes: @{NSFontAttributeName: [UIFont italicSystemFontOfSize: 12.0]}]]
But I don't want to care about the size. I just want the "default" font AND size, but in italic. Currently, I'm actually using the obliqueness attribute:
NSMutableAttributedString *text = [[NSMutableAttributedString alloc]
initWithString: @"My Text"
attributes: @{NSObliquenessAttributeName: @(0.5)}];
myLabel.attributedText = text;
This makes it so I don't have to care about whatever the size is, but I think obliqueness just approximates italics.
You can make use of [UIFont systemFontSize]
.
myLabel.attributedText = [[NSAttributedString alloc]
initWithString: @"my text"
attributes: @{
NSFontAttributeName: [UIFont italicSystemFontOfSize:
[UIFont systemFontSize]]}]];
Moreover, there are few more options to choose from:
+ (CGFloat)labelFontSize;//Returns the standard font size used for labels.
+ (CGFloat)buttonFontSize;//Returns the standard font size used for buttons.
+ (CGFloat)smallSystemFontSize;//Returns the size of the standard small system font.
+ (CGFloat)systemFontSize;//Returns the size of the standard system font.
Another option is to read the point size directly off the labels font.
myLabel.font = [UIFont italicSystemFontOfSize:myLabel.font.pointSize];
myLabel.attributedText = [[NSAttributedString alloc] initWithString: @"my text" attributes: @{NSFontAttributeName: [UIFont italicSystemFontOfSize:myLabel.font.pointSize]}];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With