質問

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]}];
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top