S'rCat
S'rCat

Reputation: 638

Can't change the text size in programmatically created UILabel

I cannot change the size of the text in a programmatically created UILabel.

Here's a screen grab:

screen grab

Here's the class definition:

class myView: UIView {
    let theLabel = UILabel()

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        self.theLabel.text = "Text"
        self.theLabel.backgroundColor = UIColor.lightGray
        self.backgroundColor = UIColor.blue

        self.theLabel.adjustsFontSizeToFitWidth = true
        self.theLabel.textAlignment = .center
        self.theLabel.numberOfLines = 1
        self.theLabel.minimumScaleFactor = 0.1
        self.theLabel.font = UIFont( name: "System", size:160)
        self.theLabel.translatesAutoresizingMaskIntoConstraints = false
        self.addSubview(self.theLabel)

        let selfAspectConstraint = NSLayoutConstraint(item: self, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.width, multiplier: 1.0, constant: 0)

        let labelWidthConstraint = NSLayoutConstraint(item: self.theLabel, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.width, multiplier: 0.5, constant: 0)

        let heightConstraint = NSLayoutConstraint(item: self.theLabel, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: self.theLabel, attribute: NSLayoutAttribute.width, multiplier: 0.5, constant: 0)

        let xConstraint = NSLayoutConstraint(item: self.theLabel, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1, constant: 0)

        let yConstraint = NSLayoutConstraint(item: self.theLabel, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1, constant: 0)

        self.addConstraints([selfAspectConstraint,labelWidthConstraint, heightConstraint,xConstraint,yConstraint])
    }
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */
}

I've tried changing the font size. I've set adjustsFontSizeToFitWidth to true and set minimumScaleFactor. The text size is always the same. I expect it to fill the label area, the grey box.

How do I get this to work?

Upvotes: 4

Views: 1386

Answers (2)

Sri
Sri

Reputation: 1336

Your code works as expected, without any change.enter image description here enter image description here

CustomView1NoXIB.swift

import UIKit
class CustomView1NoXIB : UIView {
  let theLabel = UILabel()

  required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    initSubviews()
  }




  func initSubviews() {
    self.theLabel.text = "Hello"
//    self.theLabel.text = "Hello World Big Text Becomes Small"
    self.theLabel.backgroundColor = UIColor.lightGray
    self.backgroundColor = UIColor.blue

    self.theLabel.adjustsFontSizeToFitWidth = true
    self.theLabel.textAlignment = .center
    self.theLabel.numberOfLines = 1
    self.theLabel.minimumScaleFactor = 0.1
    self.theLabel.font = UIFont( name: "System", size:160)
    self.theLabel.translatesAutoresizingMaskIntoConstraints = false
    self.addSubview(self.theLabel)

    let selfAspectConstraint = NSLayoutConstraint(item: self, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.width, multiplier: 1.0, constant: 0)

    let labelWidthConstraint = NSLayoutConstraint(item: self.theLabel, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: self, attribute: NSLayoutAttribute.width, multiplier: 0.5, constant: 0)

    let heightConstraint = NSLayoutConstraint(item: self.theLabel, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: self.theLabel, attribute: NSLayoutAttribute.width, multiplier: 0.5, constant: 0)

    let xConstraint = NSLayoutConstraint(item: self.theLabel, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1, constant: 0)

    let yConstraint = NSLayoutConstraint(item: self.theLabel, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1, constant: 0)

    self.addConstraints([selfAspectConstraint,labelWidthConstraint, heightConstraint,xConstraint,yConstraint])

  }
}

CustomView1ViewController.swift

import UIKit
class CustomView1ViewController : UIViewController {
  @IBOutlet weak var customView1: CustomView1!
  @IBOutlet weak var customView1NoXIB: CustomView1NoXIB!

  override func viewDidLoad() {
    super.viewDidLoad()
  }
}

Thanks, Sriram

Upvotes: 0

David Lindsay
David Lindsay

Reputation: 220

try adding this line after you set the font: self.theLabel.font = theLabel.font.withSize(45)

Upvotes: 3

Related Questions