How to Adjust Font Size to Fit Height and Width of 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).

How to adjust a UILabel font size based on the height available to the label

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 "adjustsFontSizeToWidth," 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).

Subclassed UILabel and overrode layoutSubviews. Then each time the UILabel gets its size changed, the font size is recalculated:

import Foundation
import UIKit

class LabelWithAdaptiveTextHeight: UILabel {

override func layoutSubviews() {
super.layoutSubviews()
font = fontToFitHeight()
}

// Returns an UIFont that fits the new label's height.
private func fontToFitHeight() -> UIFont {

var minFontSize: CGFloat = DISPLAY_FONT_MINIMUM // CGFloat 18
var maxFontSize: CGFloat = DISPLAY_FONT_BIG // CGFloat 67
var fontSizeAverage: CGFloat = 0
var textAndLabelHeightDiff: CGFloat = 0

while (minFontSize <= maxFontSize) {
fontSizeAverage = minFontSize + (maxFontSize - minFontSize) / 2

if let labelText: NSString = text {
let labelHeight = frame.size.height

let testStringHeight = labelText.sizeWithAttributes(
[NSFontAttributeName: font.fontWithSize(fontSizeAverage)]
).height

textAndLabelHeightDiff = labelHeight - testStringHeight

if (fontSizeAverage == minFontSize || fontSizeAverage == maxFontSize) {
if (textAndLabelHeightDiff < 0) {
return font.fontWithSize(fontSizeAverage - 1)
}
return font.fontWithSize(fontSizeAverage)
}

if (textAndLabelHeightDiff < 0) {
maxFontSize = fontSizeAverage - 1

} else if (textAndLabelHeightDiff > 0) {
minFontSize = fontSizeAverage + 1

} else {
return font.fontWithSize(fontSizeAverage)
}
}
}
return font.fontWithSize(fontSizeAverage)
}
}

How to adjust font size to fit height (not width) in swift 3

Swift 3...

label.minimumScaleFactor = 0.1    //you need
label.adjustsFontSizeToFitWidth = true
label.lineBreakMode = .byClipping
label.numberOfLines = 0

Best way to adjust font size with width and height of UILabel

I implemented something similar to this and the way I solved the problem is by setting the font size at a high number to begin with. adjustsFontSizeToFitWidth should work to downscale your font size according to the width.

For example, set font size at 20 and adjustsFontSizeToFitWidth will downscale the font size to fit current UILabel width.

I hope this helps.

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).

Adjust font size to fit label height - Swift 4, Xcode 9

Answering my own question. Answer found buried in the post I linked above.

If your UILabel has a fixed height, whether it be by points or by a multiplier of a superview, the font size can be set to any fraction of that height with one line of code:

testLabel.font = testLabel.font.withSize(testLabel.frame.height * 2/3)

where 2/3 is the height fraction of the label the text occupies.

Adjust UILabel height depending on the text

sizeWithFont constrainedToSize:lineBreakMode: is the method to use. An example of how to use it is below:

//Calculate the expected size based on the font and linebreak mode of your label
// FLT_MAX here simply means no constraint in height
CGSize maximumLabelSize = CGSizeMake(296, FLT_MAX);

CGSize expectedLabelSize = [yourString sizeWithFont:yourLabel.font constrainedToSize:maximumLabelSize lineBreakMode:yourLabel.lineBreakMode];

//adjust the label the the new height.
CGRect newFrame = yourLabel.frame;
newFrame.size.height = expectedLabelSize.height;
yourLabel.frame = newFrame;


Related Topics



Leave a reply



Submit