jts

JTSSmartPunctuation

A drop-in utility for adding as-you-type smart punctuation to a UITextView.

Showing:

Popularity

Downloads/wk

0

GitHub Stars

64

Maintenance

Last Commit

5yrs ago

Contributors

1

Package

Dependencies

0

License

MIT

Categories

Readme

JTSSmartPunctuation

A drop-in utility for adding as-you-type smart punctuation to a UITextView. For iOS 7 or later.

Smart What?

JTSSmartPunctuation replaces common shorthands composed of dumb punctuation with their smart counterparts. It turns dumb quotes into smart quotes, three consecutive periods into an elipsis, three consecutive dashes into an em-dash, and two consecutive dashes followed by anything but a dash with an en-dash. It’s compatible with right-to-left languages, and safe to use with composed character sequences, like emoji. It only scans the immediate vicinity around recent edits, so it should perform well even with very long runs of text.

Usage

On iOS 7 or later, a text input view like UITextView uses an instance of NSTextStorage as its backing store for the text. This text storage object takes an optional delegate that conforms to the NSTextStorageDelegate protocol. This protocol includes the following method:

- (void)textStorage:(NSTextStorage *)textStorage 
 willProcessEditing:(NSTextStorageEditActions)editedMask 
              range:(NSRange)editedRange 
     changeInLength:(NSInteger)delta;

This method is called after the user has performed a new edit to the text, but before that edit has been committed and the UI updated. Thus it is the perfect time to replace dumb punctuation with smart punctuation.

Your application will need an object that sets itself as the delegate for the text storage of your text view:

[self.textView.textStorage setDelegate:self];

Then implement the following delegate method:

- (void)textStorage:(NSTextStorage *)textStorage 
 willProcessEditing:(NSTextStorageEditActions)editedMask 
              range:(NSRange)editedRange 
     changeInLength:(NSInteger)delta {
    
    [JTSSmartPunctuation fixDumbPunctuation:textStorage
                                editedRange:editedRange
                            textInputObject:self.textView];
}

That's the basics. A more nuanced implementation also corrects the cursor position in situations where the replacement text has changed the total length:

- (void)textStorage:(NSTextStorage *)textStorage 
 willProcessEditing:(NSTextStorageEditActions)editedMask 
              range:(NSRange)editedRange 
     changeInLength:(NSInteger)delta {
    
    NSInteger lengthChange;
    
    lengthChange = [JTSSmartPunctuation fixDumbPunctuation:textStorage
                                               editedRange:editedRange
                                           textInputObject:self.textView];
                            
    if (lengthChange != 0) {
        NSRange selectedRange = [self.textView selectedRange];
        selectedRange.location += lengthChange;
        [self.textView setSelectedRange:selectedRange];
    }
}

Rate & Review

Great Documentation0
Easy to Use0
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100
No reviews found
Be the first to rate

Alternatives

No alternatives found

Tutorials

No tutorials found
Add a tutorial