无法在UILabel,Swift中包装文本

时间:2017-08-08 00:52:50

标签: ios swift uilabel

我在没有太多帮助的情况下看到了几个SO问题。

我正在使用视图控制器中的自定义类创建一个带边缘插入的UILabel,例如var message = InsetLabel()。在viewDidLoad我正在自定义标签,以包装文本,但它似乎不起作用。我哪里错了?

我没有得到全文,如下图所示:

enter image description here

class InsetLabel: UILabel {
    override func drawText(in rect: CGRect) {
        super.drawText(in: UIEdgeInsetsInsetRect(rect, UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20)))
    }
}


class ViewController: UIViewController {
var messageHeading  =   UILabel()
var message         =   InsetLabel()
var messageText  =  "Your profile is currently going through the approval process, You will be notified as soon as it has been appproved."

override func viewDidLoad() {
        super.viewDidLoad()

        messageHeading.text             =   "APPROVAL STATUS"
        messageHeading.textColor        =   Colors.darkGrey
        messageHeading.textAlignment    =   .left

        message.text                =   messageText
        message.textColor           =   Colors.darkGrey
        message.textAlignment       =   .left
        message.lineBreakMode       =   .byWordWrapping
        message.numberOfLines       =   10
        message.backgroundColor     =   Colors.white
        message.sizeToFit()
        view.addSubview(messageHeading)
        view.addSubview(message)

        messageHeading.translatesAutoresizingMaskIntoConstraints = false
        message.translatesAutoresizingMaskIntoConstraints = false

        let views = ["messageHeading"   :   messageHeading,
                     "message"          :   message
        ]

        view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-20-[messageHeading]", options: [], metrics: nil, views: views))
        view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-10-[messageHeading]-10-|", options: [], metrics: nil, views: views))
        view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:[messageHeading]-10-[message]", options: [], metrics: nil, views: views))
        view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[message]|", options: [], metrics: nil, views: views))
}
}

1 个答案:

答案 0 :(得分:0)

更改自定义标签类

class InsetLabel: UILabel {
    override func drawText(in rect: CGRect) {
        super.drawText(in: rect)
       // super.drawText(in: UIEdgeInsetsInsetRect(rect, UIEdgeInsets(top: 20, left: 20, bottom: 20, right: 20)))
    }
}

然后添加这样的约束:

  let msgHeadVerticalConst =  NSLayoutConstraint.constraints(withVisualFormat: "V:|-20-[messageHeading(>=20)]", options: [], metrics: nil, views: views)
let msgHeadHorizontalConst = NSLayoutConstraint.constraints(withVisualFormat: "H:|-10-[messageHeading]-10-|", options: [], metrics: nil, views: views)

let msgVerticalConst =          NSLayoutConstraint.constraints(withVisualFormat: "V:[messageHeading]-10-[message(>=20)]", options: [], metrics: nil, views: views)

let msgHorizontalConst = NSLayoutConstraint.constraints(withVisualFormat: "H:|-10-[message]-10-|", options: [], metrics: nil, views: views)

NSLayoutConstraint.activate(msgHeadVerticalConst + msgHeadHorizontalConst + msgVerticalConst + msgHorizontalConst)