2

I want to catch the input text in NSTextField before it changes and manipulate it.

like textField:shouldReplaceCharactersInRange for UITextField, and textView:shouldChangeTextInRange for NSTextView.

Update: I don't mind if textDidChange(notification: NSNotification) will be triggered or not

koen
  • 5,383
  • 7
  • 50
  • 89
Tal Cohen
  • 1,308
  • 10
  • 20
  • 2
    Read about [the field editor](https://developer.apple.com/library/archive/documentation/TextFonts/Conceptual/CocoaTextArchitecture/TextFieldsAndViews/TextFieldsAndViews.html#//apple_ref/doc/uid/TP40009459-CH8-BBCFEBHA). – Willeke Dec 29 '18 at 10:26
  • 1
    Possible duplicate of [How to intercept keystrokes from within the field editor of an NSTextField?](https://stackoverflow.com/questions/28901733/how-to-intercept-keystrokes-from-within-the-field-editor-of-an-nstextfield) – Willeke Dec 29 '18 at 10:27
  • 1
    Or add a custom formatter, a subclass of `NSFormatter`, and implement `isPartialStringValid:proposedSelectedRange:originalString:originalSelectedRange:errorDescription:`. – Willeke Dec 29 '18 at 10:33
  • `NSTextField` is a subclass of `NSControl` which has a `validateEditing` method. Maybe that can help you. – koen Dec 31 '18 at 14:42

2 Answers2

1

When your NSTextField is focused, it becomes the delegate of an NSTextView known as a "field editor". As a result, your NSTextField can conform to the NSTextViewDelegate protocol, which means you can implement textView:shouldChangeTextInRange:replacementString: like so:

@interface MyTextField : NSTextField <NSTextViewDelegate>
@end

@implementation MyTextField

- (BOOL)textView:(NSTextView *)textView 
  shouldChangeTextInRange:(NSRange)affectedCharRange 
  replacementString:(NSString *)replacementString
{
  // Do your thing.
}

@end
aleclarson
  • 18,087
  • 14
  • 64
  • 91
-1

How about this:

import Cocoa

class ViewController: NSViewController {

    @IBOutlet var textField: NSTextField!
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        textField.delegate = self
    }
}

extension ViewController: NSTextFieldDelegate {

    func controlTextDidChange(_ obj: Notification) {
        if let textField = obj.object as? NSTextField {
            NSLog("%@", textField.stringValue)
            // how about set the textField's max length
            textField.stringValue = String(textField.stringValue.prefix(6))
        }
    }
}
guojing
  • 129
  • 14