How to default UILabel Font and Size using Swift

爷,独闯天下 提交于 2019-11-27 03:27:16

问题


I find UISegmentedControl change font and size like this :

UISegmentedControl.appearance().setTitleTextAttributes(myFontAttribute as [NSObject : AnyObject] , forState: .Normal)

but UILabel have no this method

I want to do like

UILabel.appearance().setAttributed(myFontAttribute)

I don't want to change UILabel font in StoryBoard

I want to using program to do this (because my app is done, but only font should change to bigger and other font)

What should I do ?


回答1:


First you need to add extension to UILabel :

extension UILabel{
    var defaultFont: UIFont? {
        get { return self.font }
        set { self.font = newValue }
    }
}

Second use appearance to set it:

    UILabel.appearance().defaultFont = UIFont.systemFont(ofSize: 25)

Hope it helps.




回答2:


You can change the label font programmatically like this

label.font = UIFont(name: label.font.fontName, size: 14)

Change font size only with bold

label.font = UIFont.boldSystemFontOfSize(18)

Change font size only

label.font = label.font.fontWithSize(14)



回答3:


if you want to change the size of font, used below lines of code :

for Swift 3 :

label.font = label.font.withSize(20)



回答4:


You can use this simple code in swift

myLabel.attributedText = NSMutableAttributedString(string: myLabel.text!, attributes: [NSFontAttributeName:UIFont(name: "YourFont", size: 12), NSForegroundColorAttributeName: UIColor.whiteColor()])


来源:https://stackoverflow.com/questions/36323751/how-to-default-uilabel-font-and-size-using-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!