ios 如何在 iPhone/iPad 的 UILabel 上设置粗斜体?

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

How do I set bold and italic on UILabel of iPhone/iPad?

iosobjective-cswiftuilabeluifont

提问by Edi

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

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

采纳答案by Mobile Developer iOS Android

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

您可以设置一个字体名称列表来代替“fontWithName”属性。链接在这里

回答by Maksymilian Wojakowski

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:0means 'keep the size as is'

size:0意思是“保持原样”

With Swift try the following extension:

使用 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)
    }

}

Then you may use it this way:

那么你可以这样使用它:

myLabel.font = myLabel.font.boldItalic()

or even add additional traits like Condensed:

甚至添加额外的特征,如 Condensed:

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

Update for Swift 4:

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

myLabel.font = myLabel.font.bold

or

或者

myLabel.font = myLabel.font.italic

myLabel.font = myLabel.font.italic

or

或者

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

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

Update for Swift 5

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)
    }
}

回答by Sudhanshu

@Edinator have a look on this..

@Edinator 看看这个..

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

use any one of the above at a time you want

一次使用上述任何一种

回答by ViliusK

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

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

Label font settings

标签字体设置

回答by Even Cheng

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

回答by John Riselvato

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

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

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

Or you can simply call it:

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

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

回答by Vyacheslav

Swift 3

斯威夫特 3

Bold:

胆大:

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

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

Italic:

斜体:

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

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

回答by tolbard

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

我对 maksymilian wojakowski 的反应做了一个变体,你可以在其中添加或删除特征

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

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

回答by jayesh kavathiya

 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

你也可以使用这个来做粗体标签/按钮字体

回答by Richard Venable

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

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