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

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

This question is related to ios uilabel swift

The answer is


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)

Swift-3.1

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


Swift 4.2

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

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

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

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)


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!


In Swift 3 again...

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

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)

In Swift 3:

label = UIFont.systemFont(ofSize: 20)

and to use system preset sizes, for example:

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

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


SWIFT 3.1

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


You can give like this also

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

Swift 3

label.font.withSize(16)

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)


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)

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


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

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

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

label.font = UIFont.systemFontOfSize(20)

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.


Examples related to ios

Adding a UISegmentedControl to UITableView Crop image to specified size and picture location Undefined Symbols error when integrating Apptentive iOS SDK via Cocoapods Keep placeholder text in UITextField on input in IOS Accessing AppDelegate from framework? Autoresize View When SubViews are Added Warp \ bend effect on a UIView? Speech input for visually impaired users without the need to tap the screen make UITableViewCell selectable only while editing Xcode 12, building for iOS Simulator, but linking in object file built for iOS, for architecture arm64

Examples related to uilabel

How to make a UILabel clickable? Figure out size of UILabel based on String in Swift How to underline a UILabel in swift? Adding space/padding to a UILabel How to set textColor of UILabel in Swift Adjust UILabel height to text Setting UILabel text to bold How do I make an attributed string using Swift? How do I change the font size of a UILabel in Swift? how do I change text in a label with swift?

Examples related to swift

Make a VStack fill the width of the screen in SwiftUI Xcode 10.2.1 Command PhaseScriptExecution failed with a nonzero exit code Command CompileSwift failed with a nonzero exit code in Xcode 10 Convert Json string to Json object in Swift 4 iOS Swift - Get the Current Local Time and Date Timestamp Xcode 9 Swift Language Version (SWIFT_VERSION) How do I use Safe Area Layout programmatically? How can I use String substring in Swift 4? 'substring(to:)' is deprecated: Please use String slicing subscript with a 'partial range from' operator Safe Area of Xcode 9 The use of Swift 3 @objc inference in Swift 4 mode is deprecated?