8

With this simple code, I was assuming that getting "Hello World!" to automatically shrink would work with bannerLabel.adjustsFontSizeToFitWidth but the end of the text is clipped with ... How do you get it to work properly?

import UIKit

class ViewController: UIViewController, UIGestureRecognizerDelegate {

    @IBOutlet weak var bannerLabel: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()

        bannerLabel.adjustsFontSizeToFitWidth = true
        bannerLabel.numberOfLines = 1
        bannerLabel.text = "Hello World!"
        bannerLabel.font = UIFont(name: bannerLabel.font.fontName, size: 150)!

    }
HavelTheGreat
  • 3,299
  • 2
  • 15
  • 34
Paul S.
  • 1,342
  • 4
  • 22
  • 43
  • 3
    For one thing, you forgot to provide a `minimumScaleFactor`. This dictates how far down we can shrink. And start smaller! From your size of 150 we cannot shrink very far, so that is why we are not shrinking enough to fit the whole text in! – matt Mar 11 '15 at 20:11
  • @matt Thanks for the suggestion. Setting bannerLabel.minimumScaleFactor = 0.0 works. However it also appears that `adjustsFontSizeToFitWidth` has been deprecated in Xcode Version 6.2 (6C131e). `var adjustsFontSizeToFitWidth: Bool // default is NO // deprecated - hand tune by using NSKernAttributeName to affect tracking // NOTE: deprecated - use minimumScaleFactor. default is 0.0` – Paul S. Mar 11 '15 at 20:15

1 Answers1

12
titleLabel?.numberOfLines = 1 
titleLabel?.adjustsFontSizeToFitWidth = true
titleLabel?.minimumScaleFactor = 0.1
aturan23
  • 4,798
  • 4
  • 28
  • 52
buoge
  • 409
  • 5
  • 6