Created
August 24, 2017 09:44
-
-
Save hemangshah/80a9b497d18e9ab736026a22ae725019 to your computer and use it in GitHub Desktop.
Observe Text changes in UILabel in Swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyLabel: UILabel { | |
var textWillChange:((_ oldText: String?)->())? = nil | |
var textDidChange:((_ newText: String?)->())? = nil | |
override var text: String? { | |
willSet { | |
if textWillChange != nil { | |
textWillChange!(self.text) | |
} | |
} | |
didSet { | |
if textDidChange != nil { | |
textDidChange!(self.text) | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override func viewDidLoad() { | |
super.viewDidLoad() | |
//Usage: | |
let label = MyLabel() | |
label.textDidChange = { (text) in | |
print("New Text: \(String(describing: text))") | |
} | |
label.textWillChange = { (text) in | |
print("Old Text: \(String(describing: text))") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for the tip !