如何在不关心尺寸的情况下制作斜体UILabel?

Tra*_*ggs 4 nsattributedstring uilabel uifont ios ios7

我有一个UILabel,其textattributedText性能我更新编程.在某些情况下,我想把它用斜体.我可以:

A)设置UILabel.font属性:

myLabel.font = [UIFont italicSystemFontOfSize: 12.0];
Run Code Online (Sandbox Code Playgroud)

B)或使用属性字符串做类似的事情:

myLabel.attributedText = [[NSAttributedString alloc] initWithString: @"my text" attributes: @{NSFontAttributeName: [UIFont italicSystemFontOfSize: 12.0]}]]
Run Code Online (Sandbox Code Playgroud)

但我不想在意大小.我只想要"默认"字体和大小,但用斜体字.目前,我实际上使用的是obliqueness属性:

NSMutableAttributedString *text = [[NSMutableAttributedString alloc]
    initWithString: @"My Text"
    attributes: @{NSObliquenessAttributeName: @(0.5)}];
myLabel.attributedText = text;
Run Code Online (Sandbox Code Playgroud)

这使得我不必关心任何大小,但我认为倾斜只是近似斜体.

Rap*_*tor 6

你可以利用[UIFont systemFontSize].

myLabel.attributedText = [[NSAttributedString alloc] 
                     initWithString: @"my text" 
                         attributes: @{
              NSFontAttributeName: [UIFont italicSystemFontOfSize: 
                                         [UIFont systemFontSize]]}]];
Run Code Online (Sandbox Code Playgroud)

此外,还有更多选择可供选择:

+ (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.
Run Code Online (Sandbox Code Playgroud)