Re: override NSTextField paste behavior
Re: override NSTextField paste behavior
- Subject: Re: override NSTextField paste behavior
- From: email@hidden
- Date: Fri, 25 Mar 2016 13:55:03 +0900
What Ken is saying is use an NSFormatter
It really should have been named something like NSValidator or inherited from something like that to make it clear that it's for that.
It allows you to alter the input, accept it as is or reject it and you should present a sheet (or some other means) with some helpful information on why if rejected.
It's the right solution.
It also means you can visually show the corrected format when the content is reliably adjustable to an accepted form.
Even then, it's worth considering displaying information about the change so that users can still reconsider their input before committing to something but convenient if they're ok with the proposed reformat.
Sent from my iPhone
> On Mar 25, 2016, at 12:47 PM, Ken Thomases <email@hidden> wrote:
>
>> On Mar 24, 2016, at 9:29 PM, Rick C. <email@hidden> wrote:
>>
>> I have an NSPanel with 2 NSTextFields and my controller is the delegate of the panel. I want to override the paste behavior of the 2 text fields so that the user doesn’t paste unnecessary spaces and other characters into these fields.
>
> Is pasting really so special? Should the user be able to simply type such "unnecessary spaces and other characters" in the fields? Or do you want to prohibit that, too?
>
> Probably the easiest approach is to create a custom subclass of NSFormatter and assign an instance of that formatter to each text field. The formatter would implement -isPartialStringValid:proposedSelectedRange:originalString:originalSelectedRange:errorDescription: to validate and even adjust the string as it's being edited, regardless of the manner of the edit operation (typing, pasting, etc.).
>
> Regards,
> Ken
>
>
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden