How to adjust font size to fit height and width of UILabel
Asked Answered
F

4

8

I have a square UILabel (in yellow color) which contains a single letter.

enter image description here

I have used the following code from this SO answer to adjust the font size such that it fits into the UILabel:

letterLabel.font = UIFont(name: letterLabel.font.fontName, size: 100)
letterLabel.adjustsFontSizeToFitWidth = true
letterLabel.textAlignment = NSTextAlignment.Center

As apparent in the screenshot, the font size is according to the width. But since the text is only one letter, hence we also need to look at the height. How can we adjust the font size such that height is also within the UILabel?

Fidellas answered 22/1, 2016 at 17:29 Comment(2)
try using letterLabel.sizeToFit() Chuddar
I face exactly the same situation. Have you found the solution? If it can be done with IB, that would be twice better!Schrock
C
4

I did not find any simple solution so I made this extension:

extension UILabel {
    func setFontSizeToFill() {
        let frameSize  = self.bounds.size
        guard frameSize.height>0 && frameSize.width>0 && self.text != nil else {return}

        var fontPoints = self.font.pointSize
        var fontSize   = self.text!.size(withAttributes: [NSAttributedStringKey.font: self.font.withSize(fontPoints)])
        var increment  = CGFloat(0)

        if fontSize.width > frameSize.width || fontSize.height > frameSize.height {
            increment = -1
        } else {
            increment = 1
        }

        while true {
            fontSize = self.text!.size(withAttributes: [NSAttributedStringKey.font: self.font.withSize(fontPoints+increment)])
            if increment < 0 {
                if fontSize.width < frameSize.width && fontSize.height < frameSize.height {
                    fontPoints += increment
                    break
                }
            } else {
                if fontSize.width > frameSize.width || fontSize.height > frameSize.height {
                    break
                }
            }
            fontPoints += increment
        }

        self.font = self.font.withSize(fontPoints)
    }
}
Civilly answered 22/12, 2017 at 5:34 Comment(0)
F
2

I needed only one letter to be shown in label (name initial), so the requirement was clear, it has to scale to fit the height.

Solution:

class AnyView : UIView{
     private var nameLabel:UILabel! = nil

     override func layoutSubviews() {
        super.layoutSubviews()
        //Considering the nameLabel has been already created and added as subview with all the constraint set
        nameLabel.font = nameLabel.font.withSize(nameLabel.bounds.height * 0.6/*The factor can be adjusted as per need*/)
    }
}
Filial answered 20/6, 2018 at 16:30 Comment(0)
S
-1

I have tested your code works fine for me.enter image description here

I think cell height is the problem and I haven't give cell height. Try removing cells height

Spit answered 10/4, 2019 at 4:58 Comment(0)
G
-3

Try [label sizeToFit] or [label sizeThatFits:(CGSize)]

Gonfanon answered 22/1, 2016 at 17:44 Comment(1)
This adjusts the size of the label to fit the text, not the opposite, as was asked.Darrickdarrill

© 2022 - 2024 — McMap. All rights reserved.