将 UILabel 的文本设置为粗体

发布于 2024-10-10 13:46:58 字数 502 浏览 2 评论 0原文

我想让 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 ]];

I want to make UILabel's text bold

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 ]];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

黯然#的苍凉 2024-10-17 13:46:58

如果你想保留系统字体并将其设置为粗体:

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

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

[infoLabel setFont:[UIFont boldSystemFontOfSize:16]];
故事↓在人 2024-10-17 13:46:58

尝试

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

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

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

美人如玉 2024-10-17 13:46:58

使用 Xcode 中的 GUI 选择标签,然后转到属性检查器。选项之一是字体。单击字体图标(而不是上下箭头)。在出现的弹出窗口中展开“字体组合框”。在粗体系统部分下选择常规。

Xcode 屏幕截图

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 screenshot

智商已欠费 2024-10-17 13:46:58

对于快速用户来说,这应该可行:

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

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

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

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)
故乡的云 2024-10-17 13:46:58

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

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

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

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))
原野 2024-10-17 13:46:58

如果您没有自定义字体的粗体变体,您可以将笔划设置为负值以产生粗体效果。请参阅此处:

如何同时描边和填充与 NSAttributedString w/ UILabel

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

無心 2024-10-17 13:46:58

如果您想使用具有动态字体和粗体样式的 UILabel,请首先按如下方式扩展 UIFont。

extension UIFont {
    func withTraits(_ traits: UIFontDescriptor.SymbolicTraits) -> UIFont {
        guard let descriptor = self.fontDescriptor.withSymbolicTraits(traits) else {
            return self
        }
        return UIFont(descriptor: descriptor, size: 0) // Size 0 means to keep the size as is.
    }
}

然后为标签指定字体,如下所示:

sampleLabel.font = UIFont.preferredFont(forTextStyle: .title2).withTraits(.traitBold)

If you wanna use UILabel with a dynamic font but also with a bold style, first extend UIFont as below.

extension UIFont {
    func withTraits(_ traits: UIFontDescriptor.SymbolicTraits) -> UIFont {
        guard let descriptor = self.fontDescriptor.withSymbolicTraits(traits) else {
            return self
        }
        return UIFont(descriptor: descriptor, size: 0) // Size 0 means to keep the size as is.
    }
}

Then assign a font to label like this:

sampleLabel.font = UIFont.preferredFont(forTextStyle: .title2).withTraits(.traitBold)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文