Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set font size to fill UILabel height?

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?

like image 614
wayneh Avatar asked Jan 10 '12 23:01

wayneh


People also ask

How do you text the height of UILabel?

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.

How do I change the height of a label in Swift?

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.


2 Answers

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:

  1. User inputs expenses:

123 app

  1. When users tap the 'check' button, a menu appears from bottom, pushing everything to the top of the screen (shrinking stuff, including the label):

123 app

After the fix:

123 app

Which is exactly what the designer had in mind... :)

xScope app :)

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

like image 86
backslash-f Avatar answered Sep 21 '22 18:09

backslash-f


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 
like image 34
Kashif Avatar answered Sep 17 '22 18:09

Kashif