Is there a way to get notified when there is a change in a UILabel
\'s text or would I be better off using a UITextField
with userInteractionE
Swift 5
If you want to observe text changes in the label and make an animation. You need to create subclass UILabel
class ObservedLabelAnimate: SpringLabel {
override var text: String? {
didSet {
if let text = text {
if oldValue != text {
animation = "fadeInLeft"
duration = 1.2
animate()
}
print("This is oldvalue \(oldValue), and this is the new one \(text)")
}
}
}
}
for this example, I subclass 'SpringLabel' that inherits from UILabel
from https://github.com/MengTo/Spring
Basically it will check if the oldValue and text (new one) is different then the animation will be fired
To use:
@IBOutlet weak var label: ObservedLabelAnimate!
Create a class that inherits from UILabel. Such as:
class YourLabel: UILabel {
override var text: String? {
didSet {
if let text = text {
println("Text changed.")
} else {
println("Text not changed.")
}
}
}
}
Create a outlet of this class for your object.
@IBOutlet weak var label: YourLabel!
label.text = "abc"
You can do this simply with a subclass of UILabel:
class Label : UILabel {
override var text: String? {
didSet {
print("Text changed from \(oldValue) to \(text)")
}
}
}
oldValue
is a special value provided by Swift.
See Property Observers
For Swift 3.2 and later, the preferred method is to use a closure-based observer:
@IBOutlet public weak var label: UILabel!
var textObserver: NSKeyValueObservation?
func someAppropriateFunction() {
...
textObserver = label.observe(\.text) { [weak self] (label, observedChange) in
self?.updateStuff()
}
}
The closure will pass you the label instance and an NSKeyValueObservedChange that includes the following properties:
indexes: IndexSet?
isPrior: Bool
kind: NSKeyValueObservedChange<Value>.Kind
newValue: Value?
oldValue: Value?
First, add an observer to UILabel for key path text
.
label.addObserver(self, forKeyPath: "text", options: [.old, .new], context: nil)
Then
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if keyPath == "text" {
print("old:", change?[.oldKey])
print("new:", change?[.newKey])
}
}
label.addObserver(self, forKeyPath: "text", options: [.Old, .New], context: nil)
Then
override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
if keyPath == "text" {
print("old:", change["old"])
print("new:", change["new"])
}
}
For example console would be:
old: Optional(<null>)
new: Optional(ABC)