Ellipses and UIKit

With Sandvox 1.5 out of the door, I've had a modicum of extra free time, which I've been partly using to experiment with a rather nice framework called, er, SheepKit.

One neat trick that many desktop applications like Microsoft Office and Pages have mastered is the ability to replace certain character combinations as the user types. For example, typing three periods in a row produces a proper ellipsis character. So I decided to see how hard it would be to accomplish the same with this so-called SheepKit. Turns out it's actually pretty easy, and here's some lovely free sample code to do the job:

- (BOOL)textView:(EweEyeTextView *)textView
											        shouldChangeTextInRange:(NSRange)range
											        replacementText:(NSString *)text
											{
											    BOOL result = YES;
											    
											    if ([text hasSuffix:@"."])	// Accounts for somehow entering ".."
											    {
											        NSMutableString *resultantText = [[textView text] mutableCopy];
											        [resultantText replaceCharactersInRange:range withString:text];
											        
											        NSRange ellipsisRange =
											          NSMakeRange(range.location + [text length] - 3, 3);
											        
											        NSString *possibleEllipsis =
											          [resultantText substringWithRange:ellipsisRange];
											        
											        if ([possibleEllipsis isEqualToString:@"..."])
											        {
											            [resultantText replaceCharactersInRange:ellipsisRange
											                                         withString:@"…"];
											            
											            [textView setText:resultantText];
											            
											            NSRange selection = NSMakeRange(ellipsisRange.location + 1, 0);
											            [textView setSelectedRange:selection];
											            
											            result = NO;
											        }
											        
											        [resultantText release];
											    }
											    
											    return result;
											}
											
											

Hopefully that may be useful for some of you other *cough* SheepKit developers

© Mike Abdullah 2007-2015