无法在以编程方式创建的UILabel中更改文本大小

时间:2016-12-24 00:56:07

标签: ios swift text uilabel size

我无法在以编程方式创建的UILabel中更改文本的大小。

这是一个屏幕抓取:

screen grab

这里是班级定义:

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
    }
    */
}

我尝试过更改字体大小。我已将adjustsFontSizeToFitWidth设置为true并设置minimumScaleFactor。文本大小始终相同。我希望它能填满标签区域,灰盒子。

如何让它发挥作用?

2 个答案:

答案 0 :(得分:1)

尝试在设置字体后添加此行: self.theLabel.font = theLabel.font.withSize(45)

答案 1 :(得分:0)

您的代码按预期工作,没有任何更改。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()
  }
}

谢谢, 斯利拉姆