Git Product home page Git Product logo

brwordcounter's Introduction

BRWordCounter

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

Custom UITextViewDelegate

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

Sample App

See the CountedWords example iPhone app that comes with the source for a simple example of the code in action.

Project Integration

You can integrate BRWordCounter via CocoaPods or manually as a dependent project.

via CocoaPods

Install CocoaPods if not already available:

$ [sudo] gem install cocoapods
$ pod setup

Change to the directory of your Xcode project, and create a file named Podfile with contents similar to this:

source 'https://github.com/CocoaPods/Specs.git'

platform :ios, '7.1'

target :MyApp do
    pod 'BRWordCounter', '~> 2.0'
end

Install into your project:

$ pod install

Open your project in Xcode using the .xcworkspace file CocoaPods generated.

brwordcounter's People

Contributors

msqr avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.