ios 使 UILabel 的文本加粗

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4602586/
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 18:26:41  来源:igfitidea点击:

make UILabel's text bold

iosobjective-cuilabeluifonttextcolor

提问by Ali

I want to make UILabel's text bold

我想让 UILabel 的文字加粗

infoLabel=[[UILabel alloc]initWithFrame:CGRectMake(90,150, 200, 30)];
[infoLabel setText:@"Drag 14 more Flavors"];
[infoLabel setBackgroundColor:[UIColor clearColor]];
[infoLabel setFont:[UIFont fontWithName:@"Arial" size:16]];

[infoLabel setTextColor:[UIColor colorWithRed:193.0/255 
                                        green:27.0/255 
                                         blue:23.0/255 
                                        alpha:1 ]];

回答by Manny

If you want to retain the system font and make it bold:

如果你想保留的系统字体,并使其大胆:

[infoLabel setFont:[UIFont boldSystemFontOfSize:16]];

回答by Vladimir

Try

尝试

[infoLabel setFont:[UIFont fontWithName:@"Arial-BoldMT" size:16]];

It may also be worth checking if the font you're trying to use is available on device

检查您尝试使用的字体是否在设备上可用也可能值得

回答by Chad Kuehn

Using the GUI in Xcode select the label then go to the Attributes Inspector. One of the options is Font. Click on the font icon (not the up-down arrows). In the popup that appears expand the Font ComboxBox. Under the Bold System section choose Regular.

使用 Xcode 中的 GUI 选择标签,然后转到 Attributes Inspector。选项之一是字体。单击字体图标(而不是上下箭头)。在出现的弹出窗口中展开字体 ComboxBox。在粗体系统部分下选择常规。

Xcode screenshot

Xcode 截图

回答by Nyakiba

For swift users this should work:

对于快速用户,这应该有效:

myLabel.font = UIFont.boldSystemFont(ofSize: 12.0)

or if you'd like to use a different font:

或者如果您想使用不同的字体:

myLabel.font = UIFont(name:"HelveticaNeue-Bold", size: 12.0)

回答by Edward

Where possible I would suggest using dynamic font sizes to provide the best possible accessibility to your users.

在可能的情况下,我建议使用动态字体大小来为您的用户提供最佳的可访问性。

You can make a label use a system dynamic font and set it to have bold text by doing the following:

您可以通过执行以下操作使标签使用系统动态字体并将其设置为粗体文本:

 exampleLabel.font = UIFont.preferredFont(forTextStyle: .body, compatibleWith: UITraitCollection(legibilityWeight: .bold))

回答by Ferran Maylinch

You can set the stroke with a negative value to make a bold effect if you don't have the bold variation of your custom font. See here:

如果您没有自定义字体的粗体变体,您可以将描边设置为负值以产生粗体效果。看这里:

How can I both stroke and fill with NSAttributedString w/ UILabel

我怎样才能用带有 UILabel 的 NSAttributedString 描边和填充