Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

override UILabel setText: method in Swift

I do have the problem that I got some crashes in iOS with a UILabel subclass. Now I would like to override setText: to call layoutIfNeeded as this may solve the problem according to some stackoverflow-answers (e.g. this one).

But how can I achieve this? In Objective-C it was no big deal, but I don't find a way to override setText: in Swift.

like image 538
swalkner Avatar asked Oct 06 '15 03:10

swalkner


1 Answers

Override the property text and provide code in didSet which will get executed when the text property is set:

class MyLabel: UILabel {
     override public var text: String? {
        didSet {
            layoutIfNeeded()
        }
    }
}
like image 135
vacawama Avatar answered Sep 29 '22 07:09

vacawama