ios UILabel:你如何设置字体大小?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6294621/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:16:43  来源:igfitidea点击:

UILabel: how do you set the font size?

iosobjective-c

提问by jdl

How do you set the font size from a UILabel?

你如何从 a 设置字体大小UILabel

My code:

我的代码:

UILabel *myView = [[UILabel alloc] initWithFrame:RectFrame];
[myView setBackgroundColor:[UIColor blueColor]];
[myView setText:[NSString stringWithFormat:@" A"]];
[myView setFont:[12] ]; <--- Error
[self.view addSubview:myView];

回答by CristiC

[myView setFont:[UIFont systemFontOfSize:12]];

or

或者

[myView setFont:[UIFont boldSystemFontOfSize:12]];

or for font family

或字体系列

[myView setFont:[UIFont fontWithName:@"Helvetica" size:12]];

回答by karan

If your UILabel's font is defined in IBstoryboardand you just want to increase size then this will work, worked for me ;)

如果您UILabel的字体定义为IBstoryboard并且您只想增加大小,那么这将起作用,对我有用;)

[_label setFont: [_label.font fontWithSize: sizeYouWant]];

Or

或者

[self.label setFont: [self.label.font fontWithSize: sizeYouWant]];

Another method as others says also worked:

其他人所说的另一种方法也有效:

[_label setFont:[UIFont systemFontOfSize:13.0]];

回答by knuku

Check UIFontclass. After that you'll probably get why you should use it like that:

检查UIFont类。在那之后,您可能会明白为什么应该这样使用它:

[myView setFont:[UIFont systemFontOfSize:12]];

回答by virantporwal

UILabel *myView = [[UILabel alloc] initWithFrame:RectFrame];

[myView setBackgroundColor:[UIColor blueColor]];

[myView setText:[NSString stringWithFormat:@" A"]];

[myView setFont:[UIFont systemFontOfSize:12]];

[self.view addSubview:myView];

回答by gnasher729

Remember that the user can globally set how large they want their text to be, so you shouldn't use a fixed font size like "12". Use the following fonts:

请记住,用户可以全局设置他们希望文本的大小,因此您不应该使用像“12”这样的固定字体大小。使用以下字体:

[UIFont preferredFontForTextStyle:UIFontTextStyleHeadline]

[UIFont preferredFontForTextStyle:UIFontTextStyleBody]
[UIFont preferredFontForTextStyle:UIFontTextStyleSubheadline]
[UIFont preferredFontForTextStyle:UIFontTextStyleFootnote]
[UIFont preferredFontForTextStyle:UIFontTextStyleCaption1]
[UIFont preferredFontForTextStyle:UIFontTextStyleCaption2]

I think the default for UILabel is Caption1, the others will be bigger or smaller. And they will adapt if the user wants big text or small text on his or her device. (Font size 12 might actually bigger than the normal label size if the user picked a very small size).

我认为 UILabel 的默认值是 Caption1,其他的会更大或更小。如果用户想要在他或她的设备上使用大文本或小文本,它们会进行调整。(如果用户选择了一个非常小的尺寸,12 号字体实际上可能比正常标签尺寸大)。

回答by kamalesh kumar yadav

In swift

在迅速

yourLabel.font = UIFont(name:"name of font-family you want", size: 9)

回答by Dilip Jangid

Swift 4 and above.

Swift 4 及以上。

You may also try it.

你也可以试试。

label.font = UIFont.systemFont(ofSize: 10)