TestsTested | ✓ |
LangLanguage | Obj-CObjective C |
License | MIT |
ReleasedLast Release | Apr 2016 |
Maintained by Matt Magoffin, wmjesstaylor.
BRWordCounter is a small Objective-C helper for efficiently counting words in a UITextView
while
editing happens.
Here is an example of how to use the helper, in a UIViewController
subclass:
- (void)viewDidLoad {
[super viewDidLoad];
UITextView *textView = self.textView;
self.counter = [[BRWordCountHelper alloc] initWithTextView:textView delegate:self];
}
- (void)wordCounter:(BRWordCountHelper *)counter wordCountDidChange:(NSUInteger)count {
UILabel *wordCountLabel = self.wordCountLabel;
wordCountLabel.text = [NSString stringWithFormat:@"%lu", (unsigned long)count];
}
There is also a utility method available for counting words in a string:
[BRWordCountHelper countWordsInString:@"The 'quoted' string." finished:^(NSUInteger wordCount) {
// wordCount == 3 here
}];
By default the BRWordCountHelper
class expects to be configured as the delegate
on the
UITextView
it counts the words of. If you need to have a different delegate, you can do
so as long as you forward one delegate method on to the BRWordCountHelper
as well:
textView:shouldChangeTextInRange:replacementText:
. For example:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
// forward this call to our counter...
[counter textView:textView shouldChangeTextInRange:range replacementText:text];
// do whatever else needed here...
return YES;
}
See the CountedWords
example iPhone app that comes with the source for a simple example
of the code in action.
You can integrate BRWordCounter via CocoaPods or manually as a dependent project.