测试测试 | ✓ |
Lang语言 | Obj-CObjective C |
许可证 | MIT |
发布最后发布 | 2016年4月 |
由 Matt Magoffin,wmjesstaylor 维护。
BRWordCounter 是一个 Objective-C 高级工具,用于在编辑过程中高效地统计 UITextView
中的单词数。
以下是如何使用此工具的示例,在一个 UIViewController
子类中
- (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];
}
还有一个可用于在字符串中统计单词的实用方法
[BRWordCountHelper countWordsInString:@"The 'quoted' string." finished:^(NSUInteger wordCount) {
// wordCount == 3 here
}];
默认情况下,BRWordCountHelper
类期望以它统计单词的 UITextView
的 delegate
的形式进行配置。如果您需要不同的代理,您也可以这样做,只要你将一个代理方法转发给 BRWordCountHelper
,例如:textView:shouldChangeTextInRange:replacementText:
。例如
- (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;
}
查看附带的 CountedWords
iPhone 应用示例,该示例包含用于简单示例代码的源代码。
您可以通过 CocoaPods 或手动作为依赖项目来集成 BRWordCounter。