Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift font.withSize not changing font size on UILabel

I was having trouble changing font size in my project, so I made a playground. No matter where I put the font.withSize property, the simulator does not reflect the font size change.

import UIKit
import PlaygroundSupport

    class MyViewController : UIViewController {
        override func loadView() {
            let view = UIView()
            view.backgroundColor = .white

            let label = UILabel()
            label.font.withSize(80)
            label.frame = CGRect(x: 150, y: 200, width: 200, height: 20)
            label.text = "Hello Mom!"
            label.textColor = .black



            view.addSubview(label)
            self.view = view
        }
    }
like image 376
Chewie The Chorkie Avatar asked Apr 18 '18 15:04

Chewie The Chorkie


People also ask

How do you change text size in Xcode?

Changing the Xcode Font SizePress CMD + , Go to Font & Colors. Make sure to press CMD+A to select all possible text types. Then change the font size from the picker above.


1 Answers

withSize(_:) does not modify the font. It returns a new font with the same properties as the font you called it on, but with the new size. You have to assign the label's font to it instead:

label.font = label.font.withSize(80)
like image 111
tktsubota Avatar answered Oct 20 '22 23:10

tktsubota