I've seen a bunch of examples for changing the size of a UILabel.
Here's what I'd like to do: Change the font size so that the text will be as large as possible within the new height.
Any clues?
text = text; label. numberOfLines = 0; [label sizeToFit]; return cell; Also use NSString 's sizeWithFont:constrainedToSize:lineBreakMode: method to compute the text's height. Show activity on this post.
To give a dynamic height to an UIlabel in swift we can use the frame property of UILabel. We can create a frame using the CGRect which allows us to give different variables like x position, y position, width, and height. Let's create a label and add it as a subview to our view.
I had the very same problem and, thanks to this thread and Joel's algorithm, I could fix it. :-)
Below is my code in Swift. I'm in iOS 8 + Autolayout.
Problem:
After the fix:
Which is exactly what the designer had in mind... :)
I subclassed UILabel and overrode layoutSubviews
. Then each time the UILabel gets its size changed, the font size is recalculated:
// // LabelWithAdaptiveTextHeight.swift // 123 // // Created by https://github.com/backslash-f on 12/19/14. // /* Designed with single-line UILabels in mind, this subclass 'resizes' the label's text (it changes the label's font size) everytime its size (frame) is changed. This 'fits' the text to the new height, avoiding undesired text cropping. Kudos to this Stack Overflow thread: bit.ly/setFontSizeToFillUILabelHeight */ 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 // Abort if text happens to be nil guard text?.characters.count > 0 else { break } 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) } }
There is a simpler solution. Just add below lines and magically, the label adjusts its font size to fit the height of the label too:
SWIFT 3:
label.minimumScaleFactor = 0.1 //or whatever suits your need label.adjustsFontSizeToFitWidth = true label.lineBreakMode = .byClipping label.numberOfLines = 0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With