How to Default Uilabel Font and Size Using Swift

How to default UILabel Font and Size using Swift

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.

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

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)

iOS: set font size of UILabel Programmatically

Try [UIFont systemFontOfSize:36] or [UIFont fontWithName:@"HelveticaNeue" size:36]
i.e. [[self titleLabel] setFont:[UIFont systemFontOfSize:36]];

Swift font.withSize not changing font size on UILabel

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)

Changing UILabel's size changes its font name

.SFUIText is the default system font hence you are getting the same result when you are assigning.

Appearance does not change the properties once they are in the window.
Create custom label class and use it.

Or simply use your font UIFont(name: "PartyLetPlain", size: 17) to change the font size. it will work.

For appearance please check link https://developer.apple.com/reference/uikit/uiappearance



Related Topics



Leave a reply



Submit