How to Change the Font Size of a Uilabel in Swift

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)

change font size in UILabel based on auto layout (swift)

UILabel has no methods automatically reducing font to fit content size or height - only width.

So you should do it by manually changing font due to size changes (don't forget to test result sizes with UILabel not NSString.sizeWithFont:: results would be slightly different).

You can try to set constraints of your container to preserve UILabel's aspect ratio. This way when you reduce height, width will be reduced too and UILabel.adjustsFontSizeToFitWidth would do it's work. Should be rough, but enough in you case and much easier and faster to implement.

UPDATE

Forgot to mention. I spoke about your concrete example. Behaviour depends on numberOfLines and lineBreakMode. Making the first != 1 and second something like .byTruncatingTail (default value) will display close to necessary (but, for example, will wrap words when width isn't enough - while you expect no wrapping at all).

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)

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.

Determine font size or scale of a UILabel

You can calculate font size according to the text and width of the label.

 titleLabel.text = ""
titleLabel.alpha = 0
var charIndex = 0.0
let titleText = "Some App Title"
//You can use your custom font here.
titleLabel.font = UIFont.systemFont(ofSize: self.calculateOptimalFontSize(textLength: CGFloat(titleText.count), boundingBox: self.titleLabel.bounds))
for letter in titleText
{
Timer.scheduledTimer(withTimeInterval: (0.1 * charIndex), repeats: false)
{ (timer) in
self.titleLabel.text?.append(letter)
self.titleLabel.alpha += 0.1
}
charIndex += 1
}

You can create a similar function or extension.

func calculateOptimalFontSize(textLength:CGFloat, boundingBox:CGRect) -> CGFloat
{
let area:CGFloat = boundingBox.width * boundingBox.height
return sqrt(area / textLength)
}

Dynamically changing font size of UILabel

Single line:

factLabel.numberOfLines = 1;
factLabel.minimumFontSize = 8;
factLabel.adjustsFontSizeToFitWidth = YES;

The above code will adjust your text's font size down to (for example) 8 trying to fit your text within the label.
numberOfLines = 1 is mandatory.

Multiple lines:

For numberOfLines > 1 there is a method to figure out the size of final text through NSString's sizeWithFont:... UIKit addition methods, for example:

CGSize lLabelSize = [yourText sizeWithFont:factLabel.font
forWidth:factLabel.frame.size.width
lineBreakMode:factLabel.lineBreakMode];

After that you can just resize your label using resulting lLabelSize, for example (assuming that you will change only label's height):

factLabel.frame = CGRectMake(factLabel.frame.origin.x, factLabel.frame.origin.y, factLabel.frame.size.width, lLabelSize.height);

iOS6

Single line:

Starting with iOS6, minimumFontSize has been deprecated. The line

factLabel.minimumFontSize = 8.;

can be changed to:

factLabel.minimumScaleFactor = 8./factLabel.font.pointSize;

iOS7

Multiple lines:

Starting with iOS7, sizeWithFont becomes deprecated.
Multiline case is reduced to:

factLabel.numberOfLines = 0;
factLabel.lineBreakMode = NSLineBreakByWordWrapping;
CGSize maximumLabelSize = CGSizeMake(factLabel.frame.size.width, CGFLOAT_MAX);
CGSize expectSize = [factLabel sizeThatFits:maximumLabelSize];
factLabel.frame = CGRectMake(factLabel.frame.origin.x, factLabel.frame.origin.y, expectSize.width, expectSize.height);

iOS 13 (Swift 5):

label.adjustsFontSizeToFitWidth = true
label.minimumScaleFactor = 0.5

ios - Can we change Font, Size for all Label by single code / file?

There are a few ways to achieve that

  1. You can do something like this to give the same styling for all UILabels:
extension UILabel {
@nonobjc
convenience init() {
self.init(frame: .zero)
self.font = UIFont.systemFont(ofSize: 12, weight: UIFontWeightThin)
//... here goes other changes
}
}

You will need to use a convenience init every time.


  1. Another option would be to create some custom UILabel:
class CustomLabel: UILabel {
override init(frame: CGRect) {
super.init(frame: frame)
self.font = UIFont.systemFont(ofSize: 12, weight: UIFontWeightThin)
//... here goes other changes
}

required init?(coder: NSCoder) {
super.init(coder: coder)
self.font = UIFont.systemFont(ofSize: 12, weight: UIFontWeightThin)
//... here goes other changes
}
}

  1. The last option is relatively more complex and will require you to create customised way of handling different stylings for UILabel.

How to set font size to fill UILabel height?

Edit: Check out Joel Fischer's great answer to programmatically obtain the correct size!

You can set the font to automatically fill the size of a label, and optionally not go below a minimum font size. Just set adjustsFontSizeToFitWidth to YES. Check out the UILabel Class Reference if you need more information.

Although the boolean is called "adjustsFontSizeToFitWidth," it really means the largest size for the height of the label, that will stay on one line of the label (or however many lines you specify).



Related Topics



Leave a reply



Submit