Detect UILabel text change in swift

前端 未结 5 885
情书的邮戳
情书的邮戳 2020-12-25 15:28

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

相关标签:
5条回答
  • 2020-12-25 15:37

    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!

    0 讨论(0)
  • 2020-12-25 15:40

    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"
    
    0 讨论(0)
  • 2020-12-25 15:41

    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

    0 讨论(0)
  • 2020-12-25 15:52

    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?
    
    0 讨论(0)
  • 2020-12-25 15:56

    Swift 3

    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])
      }
    }
    

    Swift 2

    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"])
        }
    }
    

    Console Output

    For example console would be:

    old: Optional(<null>)
    new: Optional(ABC)
    
    0 讨论(0)
提交回复
热议问题