Skip to content

Instantly share code, notes, and snippets.

@fpotter
Created November 4, 2010 01:11
Show Gist options
  • Save fpotter/661987 to your computer and use it in GitHub Desktop.
Save fpotter/661987 to your computer and use it in GitHub Desktop.
@implementation PITextField : CPTextField
- (void)textDidFocus:(CPNotification)note
{
[self _inputElement].onpaste = function(e, elName)
{
var originalString = [self stringValue];
[self deleteBackward:self];
var selectedRange = [self selectedRange],
stringValue = [self stringValue],
pasteString = e.clipboardData.getData("Text"),
newValue = [stringValue stringByReplacingCharactersInRange:selectedRange withString:pasteString];
[self setStringValue:newValue];
[self setSelectedRange:CPMakeRange(selectedRange.location+pasteString.length, 0)];
var modifiedString = [self stringValue];
if (originalString !== modifiedString)
{
[self textDidChange:[CPNotification notificationWithName:CPControlTextDidChangeNotification object:self userInfo:nil]];
}
// If we don't do this, we get hit twice for some reason.
e.preventDefault();
};
}
- (void)textDidBlur:(CPNotification)note
{
[self _inputElement].onpaste = null;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment