Difference between revisions of "Snippets"
Jump to navigation
Jump to search
m |
m (Small language cleanups) |
||
Line 1: | Line 1: | ||
== Testing ''chars'' entered by users in ''NSTextField'' == | == Testing ''chars'' entered by users in ''NSTextField'' == | ||
− | + | When the contents of a NSTextField are changed by the user, it is possible to check whether or not to accept the changes. | |
1. Subclass NSTextField | 1. Subclass NSTextField | ||
− | 2. In your subclass | + | 2. In your subclass implement the |
- (BOOL) textView: (NSTextView*) textView | - (BOOL) textView: (NSTextView*) textView | ||
shouldChangeTextInRange: (NSRange) range | shouldChangeTextInRange: (NSRange) range | ||
Line 10: | Line 10: | ||
method. | method. | ||
− | 3. | + | 3. Within this method you can access to the ''replacementString'' as an NSString, or as ''char'' with the following line: |
char c = *([replacementString cString] + 0); | char c = *([replacementString cString] + 0); | ||
− | 4. | + | 4. You should return YES or NO depending on whether you accept the replacement string. |
== Limiting text input length in ''NSTextField'' == | == Limiting text input length in ''NSTextField'' == | ||
Line 20: | Line 20: | ||
1. Subclass NSTextField | 1. Subclass NSTextField | ||
− | 2. In your subclass | + | 2. In your subclass implement the |
- (BOOL) textView: (NSTextView*) textView | - (BOOL) textView: (NSTextView*) textView | ||
shouldChangeTextInRange: (NSRange) range | shouldChangeTextInRange: (NSRange) range | ||
Line 26: | Line 26: | ||
method. | method. | ||
− | 3. | + | 3. Also create an instance variable setting method: |
- (void) setMaxLength: (int) ml | - (void) setMaxLength: (int) ml | ||
{ | { | ||
Line 32: | Line 32: | ||
} | } | ||
− | 4. Then fill your | + | 4. Then fill your implementation of textView:shouldChangeTextInRange:replacementString: with the following code: |
int newLength; | int newLength; | ||
/* The exact change isn't known. Disallow the change to be safe. */ | /* The exact change isn't known. Disallow the change to be safe. */ |
Revision as of 17:03, 28 June 2005
Testing chars entered by users in NSTextField
When the contents of a NSTextField are changed by the user, it is possible to check whether or not to accept the changes.
1. Subclass NSTextField
2. In your subclass implement the
- (BOOL) textView: (NSTextView*) textView shouldChangeTextInRange: (NSRange) range replacementString: (NSString*) replacementString
method.
3. Within this method you can access to the replacementString as an NSString, or as char with the following line:
char c = *([replacementString cString] + 0);
4. You should return YES or NO depending on whether you accept the replacement string.
Limiting text input length in NSTextField
(based on Alexander Malmberg's snippet)
1. Subclass NSTextField
2. In your subclass implement the
- (BOOL) textView: (NSTextView*) textView shouldChangeTextInRange: (NSRange) range replacementString: (NSString*) replacementString
method.
3. Also create an instance variable setting method:
- (void) setMaxLength: (int) ml { maxLength = ml; }
4. Then fill your implementation of textView:shouldChangeTextInRange:replacementString: with the following code:
int newLength; /* The exact change isn't known. Disallow the change to be safe. */ if (range.location == NSNotFound) return NO; newLength=[[textView textStorage] length]+[replacementString length]-range.length; if (newLength>maxLength) return YES; //allowing else return NO; // not allowing