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

IosSwiftXcodeCocoa TouchUilabel

Ios Problem Overview


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

Ios Solutions


Solution 1 - Ios

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)

Solution 2 - Ios

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.

Solution 3 - Ios

label.font = UIFont.systemFontOfSize(20)

Solution 4 - Ios

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)

Solution 5 - Ios

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)

Solution 6 - Ios

You can give like this also

labelName.font = UIFont(name: "systemFont", size: 30)

Solution 7 - Ios

In Swift 3 again...

myLabel.font = myLabel.font.withSize(18)

Solution 8 - Ios

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

myLable.font = UIFont.systemFont(ofSize: 10)

Solution 9 - Ios

Swift-3.1

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

Solution 10 - Ios

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)

Solution 11 - Ios

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!

Solution 12 - Ios

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

Solution 13 - Ios

Swift 4.2

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

Solution 14 - Ios

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

nameLabel.font = UIFont(name: "GillSans-Bold", size: 27)

Solution 15 - Ios

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)

Solution 16 - Ios

In Swift 3:

label = UIFont.systemFont(ofSize: 20)

and to use system preset sizes, for example:

label = UIFont.systemFont(ofSize: UIFont.smallSystemFontSize)

Solution 17 - Ios

Swift 3

label.font.withSize(16)

Solution 18 - Ios

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

Solution 19 - Ios

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)

Solution 20 - Ios

SWIFT 3.1

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

Solution 21 - Ios

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJayView Question on Stackoverflow
Solution 1 - IosConnorView Answer on Stackoverflow
Solution 2 - IosmouselangeloView Answer on Stackoverflow
Solution 3 - Iosma11hew28View Answer on Stackoverflow
Solution 4 - IosGautam SareriyaView Answer on Stackoverflow
Solution 5 - IosYannStephView Answer on Stackoverflow
Solution 6 - IosSantoView Answer on Stackoverflow
Solution 7 - IosICL1901View Answer on Stackoverflow
Solution 8 - IosDilip JangidView Answer on Stackoverflow
Solution 9 - Iosaqsa arshadView Answer on Stackoverflow
Solution 10 - IoslhmgrassiView Answer on Stackoverflow
Solution 11 - IosMujtaba MahmoodView Answer on Stackoverflow
Solution 12 - IosNareshView Answer on Stackoverflow
Solution 13 - IosElshad KarimovView Answer on Stackoverflow
Solution 14 - IosfullmoonView Answer on Stackoverflow
Solution 15 - IosIkhsan AssaatView Answer on Stackoverflow
Solution 16 - IosCasey MurrayView Answer on Stackoverflow
Solution 17 - IosmattView Answer on Stackoverflow
Solution 18 - IosAdam SmakaView Answer on Stackoverflow
Solution 19 - IosZahraAsgharzadeView Answer on Stackoverflow
Solution 20 - IosIttai OrenView Answer on Stackoverflow
Solution 21 - IosFangmingView Answer on Stackoverflow