How do I change the font size of a UILabel in Swift?
Asked Answered
P

21

301

label.font.pointSize is read-only, so I'm not sure how to change it.

Plaque answered 23/6, 2014 at 0:4 Comment(1)
extension UILabel{ func font(size: CGFloat){ self.font = UIFont(descriptor: self.font.fontDescriptor, size: size) } }Affinitive
A
695

You can do it like this:

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

Or like this:

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

This will use the same font. 20 can be whatever size you want of course.

Note: The latter option will overwrite the current font weight to regular so if you want to preserve the font weight use the first option.

Swift 3 Update:

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

Swift 4 Update:

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

or

label.font = UIFont(name:"fontname", size: 20.0)

and if you use the system fonts

label.font = UIFont.systemFont(ofSize: 20.0)
label.font = UIFont.boldSystemFont(ofSize: 20.0)
label.font = UIFont.italicSystemFont(ofSize: 20.0)
Apodal answered 23/6, 2014 at 0:7 Comment(10)
This does in-fact change the pointSize property the font. However, the pointSize gets immediately reset to 17.0 before the label gets redrawn. So there's a bit more to it than that to actually see a different font size on the screen.Plaque
Are you setting the font to 17 anywhere? Storyboard?Apodal
I ran it on my iPhone 5 and your code works. It must be a bug in the simulator.Plaque
That's interesting. Not really surprised to see another bug though. Glad it's workingApodal
There's seriously something wrong with documentation when i just googled this instead of just looking it up because I knew it would be faster.Obscuration
This is good for iOS developers, but for the sake of those googling this question, I will add the note that UIFont does not exist on OSX's version of Cocoa. On OS X you'll want something like label.font=NSFont(name: label.font.fontName, size: 20)Spinel
regarding the "17", perhaps that was simply autoshrink working.Gretagretal
How do you change the font size for the label in table cells?Emancipator
extension UILabel{ func font(size: CGFloat){ self.font = UIFont(descriptor: self.font.fontDescriptor, size: size) } }Affinitive
label.font = UIFont(name:"fontname", size: 20.0) doesn't do anything in Swift 5 (at least in the emulator, can't test with a real iOS device), use label.font = label.font.withSize(20) instead.Indifference
N
73

I think the best way to do this - if keeping the same font that is already assigned to the UILabel would be:

(using Swift)

label.font = label.font.fontWithSize(20)

(using Swift 3)

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

Ideally I would set this in the viewDidLayoutSubviews method, as it doesn't need to change every time the view appears.

Notary answered 31/10, 2014 at 7:13 Comment(0)
C
40
label.font = UIFont.systemFontOfSize(20)
Crossed answered 30/1, 2015 at 16:48 Comment(2)
label.font = UIFont.systemFont(ofSize: 20) // in swift 3Balm
This solution will not work for custom fonts. It will always return system font.Maryannmaryanna
W
24

We can set font as per our requirement like,

label.font = UIFont(name: "Avenir-Light", size: 15.0)
label.font = UIFont.boldSystemFontOfSize(15)
label.font = UIFont.italicSystemFontOfSize(15)
label.font = UIFont.systemFontOfSize(17)
Walling answered 3/11, 2015 at 10:23 Comment(0)
R
10

If you want just change size of your font i create this extension

// Add extension

extension UILabel {
    func setSizeFont (sizeFont: Double) {
        self.font =  UIFont(name: self.font.fontName, size: sizeFont)!
        self.sizeToFit()
    }
}

// Use

myLabel.setSizeFont(60)
Rici answered 17/1, 2015 at 18:58 Comment(1)
This helped me. I changed the font size, its working in swift 4.Appoint
L
6

You can give like this also

labelName.font = UIFont(name: "systemFont", size: 30)
Leukoderma answered 27/10, 2014 at 13:33 Comment(0)
T
5

In Swift 3 again...

myLabel.font = myLabel.font.withSize(18)
Trace answered 23/10, 2016 at 19:41 Comment(0)
W
4

In case you want to use custom font with bold option:

nameLabel.font = UIFont(name: "GillSans-Bold", size: 27)
Wille answered 10/2, 2020 at 19:33 Comment(0)
L
3

In swift3, suppose your UILable name is myLable and you want to change its font size do this

myLable.font = UIFont.systemFont(ofSize: 10)
Latimore answered 4/2, 2017 at 9:16 Comment(0)
E
3

Swift-3.1

label.font = UIFont.systemFont(ofSize: 12)

Eppie answered 18/5, 2017 at 7:35 Comment(0)
L
2

You can use an extension.

import UIKit

extension UILabel {

    func sizeFont(_ size: CGFloat) {
        self.font = self.font.withSize(size)
    }
}

To use it:

self.myLabel.fontSize(100)
Larceny answered 6/10, 2017 at 11:43 Comment(0)
I
2

Apple keep changing things for no reason: Swift 4+:

myLabel.font = UIFont.systemFont(ofSize: 16)

thanks apple for wasting people time to figure out what "font size" methods they need to use!

Ivy answered 2/1, 2018 at 19:33 Comment(0)
B
2

Programmatically

label.font = UIFont.systemFont(ofSize: 20.0)
label.font = UIFont.boldSystemFont(ofSize: 20.0)
label.font = UIFont.italicSystemFont(ofSize: 20.0)

label.font = UIFont(name:"Helvetica Neue", size: 20.0)//Set your font name here

Through Story board

To display multiple lines set 0(Zero), this will display more than one line in your label.

If you want to display only 2 lines set 2.

enter image description here

If you want to set minimum font size for label Click Autoshrink and Select Minimum Font Size option

See below screens

enter image description here

Here set minimum font size

EX: 9 (In this image)

If your label get more text at that time your label text will be shrink upto 9

enter image description here

Bink answered 1/1, 2019 at 5:55 Comment(0)
R
2

Swift 4.2

myLabel.font = UIFont.systemFont(ofSize: 12)
Rogers answered 22/5, 2019 at 19:13 Comment(0)
S
1

I used fontWithSize for a label with light system font, but it changes back to normal system font.

If you want to keep the font's traits, better to include the descriptors.

label.font = UIFont(descriptor: label.font.fontDescriptor(), size: 16.0)

Shelley answered 6/9, 2016 at 17:5 Comment(0)
A
1

In Swift 3:

label = UIFont.systemFont(ofSize: 20)

and to use system preset sizes, for example:

label = UIFont.systemFont(ofSize: UIFont.smallSystemFontSize)
Ause answered 13/10, 2016 at 18:8 Comment(1)
I think you forgot label.fontJaved
C
0

Swift 3

label.font.withSize(16)
Courtney answered 16/3, 2017 at 7:51 Comment(0)
H
0

Swift 3.1

import UIKit

extension UILabel {
    var fontSize: CGFloat {
        get {
            return self.font.pointSize
        }
        set {
            self.font =  UIFont(name: self.font.fontName, size: newValue)!
            self.sizeToFit()
        }
    }
}
Hyades answered 26/3, 2017 at 13:12 Comment(0)
H
0

swift 4:

label.font = UIFont("your font name", size: 15)

also if you want to set the label font in all views in your project try this in appDelegate>didFinishLaunch: UILabel.appearance().font = UIFont("your font name", size: 15)

Hapten answered 28/6, 2019 at 13:8 Comment(0)
F
-1

SWIFT 3.1

Label.font = Label.font.withSize(NewValue)

Fuchsia answered 9/5, 2017 at 7:52 Comment(1)
This is not correct, and is not written using Cocoa naming conventions.Medulla
V
-1

It's very easy and convenient to change font size from storyboard, and you can instantly see the result of the change.

Actually, it's also very easy to change other font attributes on storyboard too, like style, font family, etc.

enter image description here

Valuator answered 13/7, 2017 at 17:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.