How to change font of UIButton with Swift

前端 未结 16 1270
故里飘歌
故里飘歌 2020-12-12 11:51

I am trying to change the font of a UIButton using Swift...

myButton.font = UIFont(name: \"...\", 10)

However .font is depreca

相关标签:
16条回答
  • 2020-12-12 12:13

    we can use different types of system fonts like below

    myButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 17)
    myButton.titleLabel?.font = UIFont.italicSystemFont(ofSize:UIFont.smallSystemFontSize)
    myButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: UIFont.buttonFontSize)
    

    and your custom font like below

        myButton.titleLabel?.font = UIFont(name: "Helvetica", size:12)
    
    0 讨论(0)
  • 2020-12-12 12:15

    If you are setting AttributedString to the UIButton then you can do the below thing.

    let attributedText = NSAttributedString(string: "Hello", attributes: [NSAttributedStringKey.font: UIFont(name: "Calibri", size: 19)])
    okayButton.setAttributedTitle(attributedText, for: .normal)
    
    0 讨论(0)
  • 2020-12-12 12:16

    You should go through the titleLabel property.

    button.titleLabel.font

    The font property has been deprecated since iOS 3.0.

    0 讨论(0)
  • 2020-12-12 12:18

    Take a look here.

    You should set the font of the button's titleLabel instead.

    myButton.titleLabel!.font = UIFont(name: "...", 10)
    
    0 讨论(0)
  • 2020-12-12 12:19

    This works in Swift 3.0:

    btn.titleLabel?.font = UIFont(name:"Times New Roman", size: 20) 
    
    0 讨论(0)
  • 2020-12-12 12:19

    Example: button.titleLabel?.font = UIFont(name: "HelveticaNeue-Bold", size: 12)

    • If you want to use defaul font from it's own family, use for example: "HelveticaNeue"
    • If you want to specify family font, use for example: "HelveticaNeue-Bold"
    0 讨论(0)
提交回复
热议问题