Skip to content

Commit

Permalink
some work on spell checking in tuitextview
Browse files Browse the repository at this point in the history
  • Loading branch information
joshaber committed Aug 15, 2011
1 parent 8dc3abf commit 78f47fd
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/UIKit/TUITextView.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
TUIColor *textColor;
TUITextAlignment textAlignment;
BOOL editable;

BOOL spellCheckingEnabled;
NSInteger lastSpellCheckToken;

TUIEdgeInsets contentInset;

Expand Down Expand Up @@ -62,6 +65,7 @@

@property (nonatomic, assign) NSRange selectedRange;
@property (nonatomic, assign, getter=isEditable) BOOL editable;
@property (nonatomic, assign, getter=isSpellCheckingEnabled) BOOL spellCheckingEnabled;

@property (nonatomic, copy) TUIViewDrawRect drawFrame;

Expand Down
33 changes: 33 additions & 0 deletions lib/UIKit/TUITextView.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
#import "TUITextView.h"
#import "TUITextViewEditor.h"

@interface TUITextView ()
- (void)checkSpelling;
@end

@implementation TUITextView

@synthesize delegate;
Expand All @@ -28,6 +32,7 @@ @implementation TUITextView
@synthesize editable;
@synthesize contentInset;
@synthesize placeholder;
@synthesize spellCheckingEnabled;

- (void)_updateDefaultAttributes
{
Expand Down Expand Up @@ -259,6 +264,34 @@ - (void)_textDidChange
{
if(_textViewFlags.delegateTextViewDidChange)
[delegate textViewDidChange:self];

// We only want to spell-check once they're done typing because it's super annoying to see the red wrong-spelling underline as you're typing out a word. So delay the spell check until we don't get any more text changes.
if(spellCheckingEnabled) {
static const NSTimeInterval spellCheckDelay = 0.3f;
[[self class] cancelPreviousPerformRequestsWithTarget:self selector:@selector(checkSpelling) object:nil];
[self performSelector:@selector(checkSpelling) withObject:nil afterDelay:spellCheckDelay];
}
}

- (void)checkSpelling
{
lastSpellCheckToken = [[NSSpellChecker sharedSpellChecker] requestCheckingOfString:self.text range:NSMakeRange(0, [self.text length]) types:NSTextCheckingTypeSpelling options:nil inSpellDocumentWithTag:0 completionHandler:^(NSInteger sequenceNumber, NSArray *results, NSOrthography *orthography, NSInteger wordCount) {
// This needs to happen on the main thread so that the user doesn't enter more text while we're changing the attributed string.
dispatch_async(dispatch_get_main_queue(), ^{
// we only care about the most recent results, ignore anything older
if(sequenceNumber != lastSpellCheckToken) return;

[[renderer backingStore] removeAttribute:(id)kCTUnderlineColorAttributeName range:NSMakeRange(0, [self.text length])];
[[renderer backingStore] removeAttribute:(id)kCTUnderlineStyleAttributeName range:NSMakeRange(0, [self.text length])];

for(NSTextCheckingResult *result in results) {
[[renderer backingStore] addAttribute:(id)kCTUnderlineColorAttributeName value:(id)[TUIColor redColor].CGColor range:result.range];
[[renderer backingStore] addAttribute:(id)kCTUnderlineStyleAttributeName value:[NSNumber numberWithInteger:kCTUnderlineStyleThick | kCTUnderlinePatternDot] range:result.range];
}

[self setNeedsDisplay];
});
}];
}

- (NSRange)selectedRange
Expand Down

0 comments on commit 78f47fd

Please sign in to comment.