Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to default UILabel Font and Size using Swift

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 ?

like image 279
nine9 Avatar asked Mar 31 '16 03:03

nine9


3 Answers

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.

like image 135
Oleg Sherman Avatar answered Nov 16 '22 11:11

Oleg Sherman


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)
like image 43
Rashwan L Avatar answered Nov 16 '22 11:11

Rashwan L


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

for Swift 3 :

label.font = label.font.withSize(20)
like image 2
Kiran Jadhav Avatar answered Nov 16 '22 12:11

Kiran Jadhav