标签/按钮上的约束显示标签的整个文本

时间:2018-04-24 16:38:46

标签: ios swift xcode autolayout nslayoutconstraint

Swift全新。

我希望将文字放在“开始使用”按钮上方,但要显示所有文字。在原始代码中,我有我喜欢的内容,但我无法显示整个标签文本。可以对此进行哪些编辑,以便显示整个文本?

使用原始代码:

func setupViews() {
    self.view.addSubview(lblTitle)
    lblTitle.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 150).isActive=true
    lblTitle.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive=true
    lblTitle.widthAnchor.constraint(equalToConstant: 250).isActive=true
    lblTitle.heightAnchor.constraint(equalToConstant: 80).isActive=true

    self.view.addSubview(btnGetStarted)
    btnGetStarted.heightAnchor.constraint(equalToConstant: 50).isActive=true
    btnGetStarted.widthAnchor.constraint(equalToConstant: 150).isActive=true
    btnGetStarted.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive=true
    btnGetStarted.centerYAnchor.constraint(equalTo: self.view.centerYAnchor, constant: 0).isActive=true
}

let lblTitle: UILabel = {
    let lbl=UILabel()
    lbl.text="Bacon ipsum dolor amet shankle filet mignon bacon shank frankfurter buffalo. Swine andouille burgdoggen jerky. Kevin meatball jerky tri-tip tail, biltong meatloaf fatback cupim pork chop."
    lbl.textColor=UIColor.darkGray
    lbl.textAlignment = .center
    lbl.font = UIFont.systemFont(ofSize: 46)
    lbl.numberOfLines=2
    lbl.translatesAutoresizingMaskIntoConstraints=false
    return lbl
}()

let btnGetStarted: UIButton = {
    let btn=UIButton()
    btn.setTitle("Get Started", for: .normal)
    btn.setTitleColor(UIColor.white, for: .normal)
    btn.backgroundColor=UIColor.orange
    btn.layer.cornerRadius=5
    btn.layer.masksToBounds=true
    btn.translatesAutoresizingMaskIntoConstraints=false
    btn.addTarget(self, action: #selector(btnGetStartedAction), for: .touchUpInside)
    return btn
}()

}

2 个答案:

答案 0 :(得分:2)

标签和按钮都具有minimumScaleFactor属性以及适合宽度的Bool属性。尝试使用它。

button.titleLabel?.minimumScaleFactor = 0.1
button.titleLabel?.numberOfLines = 2
button.titleLabel?.adjustsFontSizeToFitWidth = true

标签具有与缩放字体相同的属性。

 lbl.minimumScaleFactor = 0.1
 lbl.numberOfLines = 2
 lbl.adjustsFontSizeToFitWidth = true

答案 1 :(得分:0)

  1. 尝试更改标签的height并将topAnchor添加到按钮
  2. adjustsFontSizeToFitWidthnumberOfLines=0sizeToFit()添加到标签

    func setupViews() {
        self.view.addSubview(lblTitle)
        lblTitle.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 150).isActive=true
        lblTitle.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive=true
        lblTitle.widthAnchor.constraint(equalToConstant: 250).isActive=true
        lblTitle.heightAnchor.constraint(equalToConstant: 250).isActive=true
    
        self.view.addSubview(btnGetStarted)
        btnGetStarted.topAnchor.constraint(equalTo: lblTitle.bottomAnchor, constant: 20).isActive=true
        btnGetStarted.heightAnchor.constraint(equalToConstant: 50).isActive=true
        btnGetStarted.widthAnchor.constraint(equalToConstant: 150).isActive=true
        btnGetStarted.centerXAnchor.constraint(equalTo: self.view.centerXAnchor).isActive=true
        btnGetStarted.centerYAnchor.constraint(equalTo: self.view.centerYAnchor, constant: 0).isActive=true
    }
    
    let lblTitle: UILabel = {
        let lbl=UILabel()
        lbl.text="Bacon ipsum dolor amet shankle filet mignon bacon shank frankfurter buffalo. Swine andouille burgdoggen jerky. Kevin meatball jerky tri-tip tail, biltong meatloaf fatback cupim pork chop."
        lbl.textColor=UIColor.darkGray
        lbl.textAlignment = .center
        lbl.font = UIFont.systemFont(ofSize: 46)
        lbl.adjustsFontSizeToFitWidth = true
        lbl.numberOfLines=0
        lbl.sizeToFit()
        lbl.translatesAutoresizingMaskIntoConstraints=false
        return lbl
    }()