3

How is it possible to apply a word limit to a UITextView in objective-c/interface builder.?

I have been searching for a while and have found character count but not word count...

Cany anybody give me any pointers...

user559142
  • 12,279
  • 49
  • 116
  • 179

3 Answers3

6

You can just count the number of spaces and restrict that. It's a hack, but it works.

You can do this inside

- (BOOL)textView:(UITextView *)aTextView shouldChangeTextInRange:(NSRange)aRange replacementText:(NSString*)aText

by using one of these NSString methods

- (NSArray *)componentsSeparatedByCharactersInSet:(NSCharacterSet *)separator
- (NSString *)stringByReplacingOccurrencesOfString:(NSString *)target withString:(NSString *)replacement

For example, you can do something along these lines:

- (BOOL)textView:(UITextView *)aTextView shouldChangeTextInRange:(NSRange)aRange replacementText:(NSString*)aText {
        NSString* newText = [aTextView.text stringByReplacingCharactersInRange:aRange withString:aText];
        NSString *trimmedText = [newText stringByReplacingOccurrencesOfString:@" " withString:@""];

        if (newText.length - trimmedText.length > wordLimit) {
            return NO;
        } else {
            return YES;
        }
}

If you want to be more accurate, you can also "fix" the text first by replacing multiple spaces with a single space and inserting a space after punctuation. This should probably be written as a separate function that you call on the input text.

PengOne
  • 48,188
  • 17
  • 130
  • 149
  • do I have to add to the containing classes interface declaration? – user559142 Aug 11 '11 at 20:55
  • @user559142: Just make sure the containing class is the delegate of the `UITextView` and you should be all set. – PengOne Aug 11 '11 at 20:59
  • when setting the delegate, it doesn't display the text in the UiTextView...the cursor just blinks but when I type no text appears – user559142 Aug 11 '11 at 21:02
  • @user559142: Log what's happening in the method above. It may be there is an error in the code, and this method is always returning `NO`. – PengOne Aug 11 '11 at 21:36
2

If you're using iOS 4.0 or later, you might be able to get better results using NSRegularExpression. I haven't tried this out, but something like the following should be close:

NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"\\w+"
                                                                       options:0
                                                                         error:&error];
NSUInteger wordCount = [regex numberOfMatchesInString:string
                                              options:0
                                                range:NSMakeRange(0, [string length])];
Tim Dean
  • 8,253
  • 2
  • 32
  • 59
0

Another idea besides counting the spaces is to use the average word length of 5 characters + 1 space to determine the number of words. In other words, take your total character count divided by 6 to get your estimated word count. It's a bit faster that way if you're ok with it not being 100% accurate.

Ken Pespisa
  • 21,989
  • 3
  • 55
  • 63
  • I'd actually set that at 6 + 1 to account for punctuation. Typically, an accurate word-counting routine has negligible impacts on performance with modern CPUs. I don't see why any one would use this method, in a real project. – Benjamin Mayo Aug 11 '11 at 22:31