我该如何在不改变字体的情况下更改uilabel的字体大小?

7
我遇到一个问题,我的项目中允许用户选择标签的字体,但是当用户使用不同的按钮设置标签大小时,标签会重置为默认字体。我希望能够保留用户应用的字体,同时允许用户更改字体大小。非常感谢任何帮助!以下是我的代码:
-(IBAction)setFont{

    [userText setFont:[UIFont fontWithName:@"Arial-BoldMT" size:50.0]];

//I had to add a size when setting the font or else I got an error


}



-(IBAction)setFontSize{

  [userText setFont:[UIFont systemFontOfSize:24]];

}
2个回答

31

只需在标签的当前字体上使用 fontWithSize: 方法:

- (IBAction)setFontSize {
    // Keep the same font but change its size to 24 points.
    UIFont *font = userText.font;
    userText.font = [font fontWithSize:24];
}

1
函数[UIFont systemFontOfSize:]将始终返回默认系统字体。您可以直接使用在setFont中调用的相同函数[UIFont fontWithName:size:]

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接