I'm not able to find any inbuilt property for this. Is there a way by which I can manually achieve this? How to set maximum length of a NSTextField without overwriting the last character ? They shouldn't be able to enter the next character after maximum limit is reached.
Asked
Active
Viewed 241 times
-3
-
2Possible duplicate of [Set maximum characters (to one) in a NSTextfield in Swift](https://stackoverflow.com/questions/35589503/set-maximum-characters-to-one-in-a-nstextfield-in-swift) – Willeke Jun 23 '19 at 08:28
-
1The answers for the one you mentioned are not convincing.I'm not considering this as a duplicate since no convincing answers are found – Siddharth Venu Jun 23 '19 at 08:42
-
1exactly, those solutions aren't properly written – Prithvi Venu Jun 23 '19 at 08:51
2 Answers
0
Hope this will help you,
-(void)controlTextDidChange:(NSNotification *)obj{
if([Textfield .stringValue length]>8)//you can set the maximum
{
[[Textfield currentEditor]deleteBackward:nil];
}
}
**Note:Set your textfiled's delegate to self. Eg:textfiled.delegate = self;*

Shiva_Kishore
- 39
- 10
-1
NSTextField delegate have a function that you can use to check length of text
- (BOOL)textView:(NSTextView *)aTextView shouldChangeTextInRange:(NSRange)affectedCharRange replacementString:(NSString *)replacementString
{
}
If you don't want to apply this delegate function every where you use NSTextField
. You can subclass a CustomTextField.
Hope this help.

Tung Vu Duc
- 1,552
- 1
- 13
- 22