BRWordCounter 2.0.1

BRWordCounter 2.0.1

测试测试
Lang语言 Obj-CObjective C
许可证 MIT
发布最后发布2016年4月

Matt Magoffinwmjesstaylor 维护。



  • Matt Magoffin

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
}];

自定义 UITextViewDelegate

默认情况下,BRWordCountHelper 类期望以它统计单词的 UITextViewdelegate 的形式进行配置。如果您需要不同的代理,您也可以这样做,只要你将一个代理方法转发给 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。