iPhone/iPad的UILabel如何设置粗体和斜体?

发布于 2024-10-12 09:21:05 字数 76 浏览 6 评论 0原文

如何在iPhone/iPad的UILabel上设置粗体和斜体? 我搜索了论坛,但没有任何帮助。有人可以帮助我吗?

How do I set bold and italic on UILabel of iPhone/iPad?
I searched the forum but nothing helped me. Could anyone help me?

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

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

发布评论

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

评论(19

清风疏影 2024-10-19 09:21:06

我最近写了一篇关于 UIFont API 的限制以及如何解决的博文。
您可以在此处

使用我在那里提供的代码,您可以轻松获得所需的 UIFont,如下所示:

UIFont *myFont = [FontResolver fontWithDescription:@"font-family: Helvetica; font-weight: bold; font-style : italic;"];

然后将其设置为您的 UILabel (或其他):
myLabel.font = myFont;

I recently wrote a blog post about the restrictions of the UIFont API and how to solve it.
You can see it at here

With the code I provide there, you can get your desired UIFont as easy as:

UIFont *myFont = [FontResolver fontWithDescription:@"font-family: Helvetica; font-weight: bold; font-style: italic;"];

And then set it to your UILabel (or whatever) with:
myLabel.font = myFont;

靖瑶 2024-10-19 09:21:06

正如这些答案中已经提到的,您只需要正确的字体名称。我发现 iOSFonts.com 是准确了解要使用的名称的最有用的资源。

As has already been mentioned in these answers, you just need the right font name. I find that iOSFonts.com is the most helpful resource for knowing exactly what name to use.

优雅的叶子 2024-10-19 09:21:06

这很简单。这是代码。

[yourLabel setFont:[UIFont boldSystemFontOfSize:15.0];

这会对你有所帮助。

This is very simple. Here is the code.

[yourLabel setFont:[UIFont boldSystemFontOfSize:15.0];

This will help you.

怼怹恏 2024-10-19 09:21:06

很多时候,粗体文本被视为另一级别的信息架构方式,因此一行中没有粗体和常规,因此您可以将其拆分为两个标签/文本视图,一个常规,一个粗体斜体。并使用编辑器选择字体样式。

Many times the bolded text is regarded in an information architecture way on another level and thus not have bolded and regular in one line, so you can split it to two labels/textViews, one regular and on bold italic. And use the editor to choose the font styles.

锦爱 2024-10-19 09:21:06

使用Swift 5

对于样式=粗体

label.font = UIFont(name:"HelveticaNeue-Bold", size: 15.0)

对于样式 = 中

label.font = UIFont(name:"HelveticaNeue-Medium", size: 15.0)

风格=薄

label.font = UIFont(name:"HelveticaNeue-Thin", size: 15.0)

With Swift 5

For style = BOLD

label.font = UIFont(name:"HelveticaNeue-Bold", size: 15.0)

For style = Medium

label.font = UIFont(name:"HelveticaNeue-Medium", size: 15.0)

For style = Thin

label.font = UIFont(name:"HelveticaNeue-Thin", size: 15.0)
乖乖兔^ω^ 2024-10-19 09:21:06

只是想提一下,

UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.bold)
UIFont.boldSystemFont(ofSize: 16)

他们有不同的结果......

Just want to mention that,

UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.bold)
UIFont.boldSystemFont(ofSize: 16)

They have different outcome...

愛上了 2024-10-19 09:21:06

针对 Swift 5 进行了更新,并尊重设备的首选尺寸类别。

// Bold
label.font = UIFont.systemFont(ofSize: UIFont.preferredFont(forTextStyle: .body).pointSize, weight: .bold)

// Italic
label.font = UIFont.italicSystemFont(ofSize: UIFont.preferredFont(forTextStyle: .body).pointSize)

这里发布的其他答案都很好,但有硬编码的 pointSizes。

Updated for Swift 5 and respects the device's preferred size category.

// Bold
label.font = UIFont.systemFont(ofSize: UIFont.preferredFont(forTextStyle: .body).pointSize, weight: .bold)

// Italic
label.font = UIFont.italicSystemFont(ofSize: UIFont.preferredFont(forTextStyle: .body).pointSize)

The other answers posted here are good, but have hard-coded pointSizes.

梦幻的味道 2024-10-19 09:21:05

不要尝试使用字体名称。使用字体描述符,您不需要名称:

UILabel * label = [[UILabel alloc] init]; // use your label object instead of this
UIFontDescriptor * fontD = [label.font.fontDescriptor
            fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitBold
                            | UIFontDescriptorTraitItalic];
label.font = [UIFont fontWithDescriptor:fontD size:0];

size:0 表示“保持原样大小”

使用 Swift 尝试以下扩展:

extension UIFont {

    func withTraits(traits:UIFontDescriptorSymbolicTraits...) -> UIFont {
        let descriptor = self.fontDescriptor()
            .fontDescriptorWithSymbolicTraits(UIFontDescriptorSymbolicTraits(traits))
        return UIFont(descriptor: descriptor, size: 0)
    }

    func boldItalic() -> UIFont {
        return withTraits(.TraitBold, .TraitItalic)
    }

}

然后您可以这样使用它:

myLabel.font = myLabel.font.boldItalic()

甚至添加其他特征,例如 Condensed:

myLabel.font = myLabel.font.withTraits(.TraitCondensed, .TraitBold, .TraitItalic)

Swift 4 更新:

extension UIFont {
  var bold: UIFont {
    return with(traits: .traitBold)
  } // bold

  var italic: UIFont {
    return with(traits: .traitItalic)
  } // italic

  var boldItalic: UIFont {
    return with(traits: [.traitBold, .traitItalic])
  } // boldItalic


  func with(traits: UIFontDescriptorSymbolicTraits) -> UIFont {
    guard let descriptor = self.fontDescriptor.withSymbolicTraits(traits) else {
      return self
    } // guard

    return UIFont(descriptor: descriptor, size: 0)
  } // with(traits:)
} // extension

按如下方式使用:

myLabel.font = myLabel.font.bold

myLabel.font = myLabel.font.italic

myLabel.font = myLabel.font.with(traits: [ .traitBold, .traitCondensed ])

Swift 5 更新

extension UIFont {
    var bold: UIFont {
        return with(.traitBold)
    }

    var italic: UIFont {
        return with(.traitItalic)
    }

    var boldItalic: UIFont {
        return with([.traitBold, .traitItalic])
    }



    func with(_ traits: UIFontDescriptor.SymbolicTraits...) -> UIFont {
        guard let descriptor = self.fontDescriptor.withSymbolicTraits(UIFontDescriptor.SymbolicTraits(traits).union(self.fontDescriptor.symbolicTraits)) else {
            return self
        }
        return UIFont(descriptor: descriptor, size: 0)
    }

    func without(_ traits: UIFontDescriptor.SymbolicTraits...) -> UIFont {
        guard let descriptor = self.fontDescriptor.withSymbolicTraits(self.fontDescriptor.symbolicTraits.subtracting(UIFontDescriptor.SymbolicTraits(traits))) else {
            return self
        }
        return UIFont(descriptor: descriptor, size: 0)
    }
}

Don't try to play with the font names. Using the font descriptor you need no names:

UILabel * label = [[UILabel alloc] init]; // use your label object instead of this
UIFontDescriptor * fontD = [label.font.fontDescriptor
            fontDescriptorWithSymbolicTraits:UIFontDescriptorTraitBold
                            | UIFontDescriptorTraitItalic];
label.font = [UIFont fontWithDescriptor:fontD size:0];

size:0 means 'keep the size as is'

With Swift try the following extension:

extension UIFont {

    func withTraits(traits:UIFontDescriptorSymbolicTraits...) -> UIFont {
        let descriptor = self.fontDescriptor()
            .fontDescriptorWithSymbolicTraits(UIFontDescriptorSymbolicTraits(traits))
        return UIFont(descriptor: descriptor, size: 0)
    }

    func boldItalic() -> UIFont {
        return withTraits(.TraitBold, .TraitItalic)
    }

}

Then you may use it this way:

myLabel.font = myLabel.font.boldItalic()

or even add additional traits like Condensed:

myLabel.font = myLabel.font.withTraits(.TraitCondensed, .TraitBold, .TraitItalic)

Update for Swift 4:

extension UIFont {
  var bold: UIFont {
    return with(traits: .traitBold)
  } // bold

  var italic: UIFont {
    return with(traits: .traitItalic)
  } // italic

  var boldItalic: UIFont {
    return with(traits: [.traitBold, .traitItalic])
  } // boldItalic


  func with(traits: UIFontDescriptorSymbolicTraits) -> UIFont {
    guard let descriptor = self.fontDescriptor.withSymbolicTraits(traits) else {
      return self
    } // guard

    return UIFont(descriptor: descriptor, size: 0)
  } // with(traits:)
} // extension

Use it as follows:

myLabel.font = myLabel.font.bold

or

myLabel.font = myLabel.font.italic

or

myLabel.font = myLabel.font.with(traits: [ .traitBold, .traitCondensed ])

Update for Swift 5

extension UIFont {
    var bold: UIFont {
        return with(.traitBold)
    }

    var italic: UIFont {
        return with(.traitItalic)
    }

    var boldItalic: UIFont {
        return with([.traitBold, .traitItalic])
    }



    func with(_ traits: UIFontDescriptor.SymbolicTraits...) -> UIFont {
        guard let descriptor = self.fontDescriptor.withSymbolicTraits(UIFontDescriptor.SymbolicTraits(traits).union(self.fontDescriptor.symbolicTraits)) else {
            return self
        }
        return UIFont(descriptor: descriptor, size: 0)
    }

    func without(_ traits: UIFontDescriptor.SymbolicTraits...) -> UIFont {
        guard let descriptor = self.fontDescriptor.withSymbolicTraits(self.fontDescriptor.symbolicTraits.subtracting(UIFontDescriptor.SymbolicTraits(traits))) else {
            return self
        }
        return UIFont(descriptor: descriptor, size: 0)
    }
}
青巷忧颜 2024-10-19 09:21:05
sectionLabel.font = [UIFont fontWithName:@"TrebuchetMS-Bold" size:18];

您可以设置一个字体名称列表来代替“fontWithName”属性。链接位于此处

sectionLabel.font = [UIFont fontWithName:@"TrebuchetMS-Bold" size:18];

There is a list of font names that you can set in place of 'fontWithName' attribute.The link is here

和影子一齐双人舞 2024-10-19 09:21:05

@Edinator 看看这个..

myLabel.font = [UIFont boldSystemFontOfSize:16.0f]
myLabel.font = [UIFont italicSystemFontOfSize:16.0f];

一次使用上面的任何一个你想要的

@Edinator have a look on this..

myLabel.font = [UIFont boldSystemFontOfSize:16.0f]
myLabel.font = [UIFont italicSystemFontOfSize:16.0f];

use any one of the above at a time you want

厌倦 2024-10-19 09:21:05

您可以通过单击字体字段中的字母“T”来设置标签的任何字体样式、系列、大小。

标签字体设置

You can set any font style, family, size for the label, by clicking on letter "T" in Font field.

Label font settings

同展鸳鸯锦 2024-10-19 09:21:05

Swift 3

粗体:

let bondFont = UIFont.boldSystemFont(ofSize:UIFont.labelFontSize)

斜体:

让 italicFont = UIFont.italicSystemFont(ofSize:UIFont.labelFontSize)

Swift 3

Bold:

let bondFont = UIFont.boldSystemFont(ofSize:UIFont.labelFontSize)

Italic:

let italicFont = UIFont.italicSystemFont(ofSize:UIFont.labelFontSize)

凶凌 2024-10-19 09:21:05

对于 iOS 7 系统默认字体,如果您希望保留系统默认字体,您将使用 helvetica neue 粗体。

    [titleText setFont:[UIFont fontWithName:@"HelveticaNeue-Bold" size:16.0]];

或者你可以简单地调用它:

    [titleText setFont:[UIFont boldSystemFontOfSize:16.0]];

With iOS 7 system default font, you'll be using helvetica neue bold if you are looking to keep system default font.

    [titleText setFont:[UIFont fontWithName:@"HelveticaNeue-Bold" size:16.0]];

Or you can simply call it:

    [titleText setFont:[UIFont boldSystemFontOfSize:16.0]];
二智少女 2024-10-19 09:21:05

我有同样的问题,需要在标签和按钮上应用粗体和斜体。您可以简单地使用以下代码来实现此效果:

myLabel.font = [UIFont fontWithName:@"Arial-BoldItalic" size:30.0];

I have the same issue that need to apply both Bold and Italic on a label and button. You can simply use following code to achieve this effect:

myLabel.font = [UIFont fontWithName:@"Arial-BoldItalic" size:30.0];
牛↙奶布丁 2024-10-19 09:21:05

我对马克西米利安·沃贾科夫斯基的反应做了一个变体
您可以在其中添加或删除特征,

extension UIFont {

    func withTraits(_ traits:UIFontDescriptorSymbolicTraits...) -> UIFont {
        let descriptor = self.fontDescriptor
            .withSymbolicTraits(UIFontDescriptorSymbolicTraits(traits).union(self.fontDescriptor.symbolicTraits))
        return UIFont(descriptor: descriptor!, size: 0)
    }
    func withoutTraits(_ traits:UIFontDescriptorSymbolicTraits...) -> UIFont {
        let descriptor = self.fontDescriptor
            .withSymbolicTraits(  self.fontDescriptor.symbolicTraits.subtracting(UIFontDescriptorSymbolicTraits(traits)))
        return UIFont(descriptor: descriptor!, size: 0)
    }
    func bold() -> UIFont {
        return withTraits( .traitBold)
    }

    func italic() -> UIFont {
        return withTraits(.traitItalic)
    }

    func noItalic() -> UIFont {
        return withoutTraits(.traitItalic)
    }
    func noBold() -> UIFont {
        return withoutTraits(.traitBold)
    }
}

例如

label.font = label.font.italic().bold()

,当重复使用单元格并且您想要删除在前一个单元格中的标签上放置的斜体时,它很有用

I made a variation of the response of maksymilian wojakowski
where you can add or remove a trait(s)

extension UIFont {

    func withTraits(_ traits:UIFontDescriptorSymbolicTraits...) -> UIFont {
        let descriptor = self.fontDescriptor
            .withSymbolicTraits(UIFontDescriptorSymbolicTraits(traits).union(self.fontDescriptor.symbolicTraits))
        return UIFont(descriptor: descriptor!, size: 0)
    }
    func withoutTraits(_ traits:UIFontDescriptorSymbolicTraits...) -> UIFont {
        let descriptor = self.fontDescriptor
            .withSymbolicTraits(  self.fontDescriptor.symbolicTraits.subtracting(UIFontDescriptorSymbolicTraits(traits)))
        return UIFont(descriptor: descriptor!, size: 0)
    }
    func bold() -> UIFont {
        return withTraits( .traitBold)
    }

    func italic() -> UIFont {
        return withTraits(.traitItalic)
    }

    func noItalic() -> UIFont {
        return withoutTraits(.traitItalic)
    }
    func noBold() -> UIFont {
        return withoutTraits(.traitBold)
    }
}

exemple

label.font = label.font.italic().bold()

it useful when reusing cell and you want to remove the italic you put on a label in a previous cell

余生再见 2024-10-19 09:21:05
 btn.titleLabel.font=[UIFont fontWithName:@"Helvetica neue" size:10];
 btn.titleLabel.font =  [UIFont boldSystemFontOfSize:btnPrev.titleLabel.font.pointSize+3];

您也可以使用它来制作粗体标签/按钮字体

 btn.titleLabel.font=[UIFont fontWithName:@"Helvetica neue" size:10];
 btn.titleLabel.font =  [UIFont boldSystemFontOfSize:btnPrev.titleLabel.font.pointSize+3];

you can do bold label/button font also using this

九厘米的零° 2024-10-19 09:21:05

粗体文本示例:

UILabel *titleBold = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 200, 30)];
UIFont* myBoldFont = [UIFont boldSystemFontOfSize:[UIFont systemFontSize]];
[titleBold setFont:myBoldFont];

斜体文本示例:

UILabel *subTitleItalic = [[UILabel alloc] initWithFrame:CGRectMake(10, 35, 200, 30)];
UIFont* myItalicFont = [UIFont italicSystemFontOfSize:[UIFont systemFontSize]];
[subTitleItalic setFont:myItalicFont];

Example Bold text:

UILabel *titleBold = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 200, 30)];
UIFont* myBoldFont = [UIFont boldSystemFontOfSize:[UIFont systemFontSize]];
[titleBold setFont:myBoldFont];

Example Italic text:

UILabel *subTitleItalic = [[UILabel alloc] initWithFrame:CGRectMake(10, 35, 200, 30)];
UIFont* myItalicFont = [UIFont italicSystemFontOfSize:[UIFont systemFontSize]];
[subTitleItalic setFont:myItalicFont];
久夏青 2024-10-19 09:21:05

尽管@tolbard 提供的答案令人惊叹并且效果很好!

我觉得为只需一行代码即可实现的东西创建一个扩展将是一种过度的杀伤力。

通过使用 UIFontDescriptor 设置字体属性,您可以为 label 中的相同文本获得粗体以及斜体样式> 如下例所示,使用 Swift 4.0

label.font = UIFont(descriptor: UIFontDescriptor().withSymbolicTraits([.traitBold, .traitItalic])!, size: 12)

其他选项包括:

traitLooseLeading
traitTightLeading
traitUIOptimized
traitVertical
traitMonoSpace
traitCondensed
traitExpanded

有关这些符号特征含义的更多信息? 访问此处

Although the answer provided by @tolbard is amazing and works well!

I feel creating an extension for something that can be achieved in just a line of code, would be an over kill.

You can get bold as well italic styling for the same text in your label by setting up the font property using UIFontDescriptor as shown below in the example below using Swift 4.0:

label.font = UIFont(descriptor: UIFontDescriptor().withSymbolicTraits([.traitBold, .traitItalic])!, size: 12)

Other options include:

traitLooseLeading
traitTightLeading
traitUIOptimized
traitVertical
traitMonoSpace
traitCondensed
traitExpanded

For more information on what those symbolic traits mean? visit here

凉薄对峙 2024-10-19 09:21:05

更新 Maksymilian Wojakowski 对 swift 3 的精彩答案

extension UIFont {
    func withTraits(traits:UIFontDescriptorSymbolicTraits...) -> UIFont? {
        guard let descriptorL = self.fontDescriptor.withSymbolicTraits(UIFontDescriptorSymbolicTraits(traits)) else{
            return nil
        }
        return UIFont(descriptor: descriptorL, size: 0)
    }

    func boldItalic() -> UIFont? {
        return withTraits(traits: .traitBold, .traitItalic)
    }
}

Updating Maksymilian Wojakowski's awesome answer for swift 3

extension UIFont {
    func withTraits(traits:UIFontDescriptorSymbolicTraits...) -> UIFont? {
        guard let descriptorL = self.fontDescriptor.withSymbolicTraits(UIFontDescriptorSymbolicTraits(traits)) else{
            return nil
        }
        return UIFont(descriptor: descriptorL, size: 0)
    }

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