Make NSFormatter check NSTextFieldCell continuously

In Cocoa, I have an NSOutlineView where the cells are NSTextFieldCell. A cell displays values, which are strings that are formatted according to specific rules (such as floats or pairs of floats with a gap between them). I created a custom NSFormatter to check the text, and it seems to be no problem.

However, the cell (or the representation of the circuit, I'm not sure what causes this) seems to only use the formatter at the time my editing is complete. If I enter some text characters in a text box (which violates the formatting rules), these characters appear - the only way to notice that the formatting operator is doing its job, so I can no longer move the keyboard focus from this cell. If I return the contents of the cell to its actual form, then I can move the focus.

I set both the cell and the circuit to be "continuous."

It would be better if I could not enter the text in the cell in the first place. Is it possible to do so, and if so, how?

+4
source share
1 answer

Answering my own question because I found a solution. There is an optional override method on NSFormatter , and this solves the problem. Optional method:

 - (BOOL) isPartialStringValid: (NSString*) partialString newEditingString: (NSString**) newString errorDescription: (NSString**) error 

Here you can simply return NO if partialString not valid. You can return a fixed string by reference in newString if you wish.

There is another method that can be used, but it is more complex:

 - (BOOL) isPartialStringValid: (NSString**) partialStringPtr proposedSelectedRange: (NSRangePointer) proposedSelRangePtr originalString: (NSString*) origString originalSelectedRange: (NSRange) origSelRange errorDescription: (NSString**) error 
+7
source

Source: https://habr.com/ru/post/1310229/


All Articles