Created
December 9, 2016 10:43
-
-
Save NikhilManapure/c4509118a6b562964290b80a26d3fad2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
+ (NSString*)stringByRemovingEmojiFrom:(NSString*)string { | |
NSMutableString* __block buffer = [NSMutableString stringWithCapacity:[string length]]; | |
[string enumerateSubstringsInRange:NSMakeRange(0, [string length]) | |
options:NSStringEnumerationByComposedCharacterSequences | |
usingBlock: ^(NSString* substring, NSRange substringRange, NSRange enclosingRange, BOOL* stop) { | |
[buffer appendString:([[self class] isEmoji:substring])? @"": substring]; | |
}]; | |
return buffer; | |
} | |
+ (BOOL)isEmoji:(NSString*)string { | |
//I will suggest to create this once and use it instead of creating it everytime like this. | |
NSMutableCharacterSet *alphaNumericWhitespaceCharacterSet = [[NSCharacterSet alphanumericCharacterSet] mutableCopy]; | |
[alphaNumericWhitespaceCharacterSet formUnionWithCharacterSet:[NSCharacterSet whitespaceCharacterSet]]; | |
// Union with other charsets if want to include other charachters. | |
return [string rangeOfCharacterFromSet:alphaNumericWhitespaceCharacterSet].location == NSNotFound; | |
} | |
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string | |
{ | |
if (string.length != 0) { | |
string = [[self class] stringByRemovingEmojiFrom:string]; | |
textField.text = [textField.text stringByReplacingCharactersInRange:range withString:string]; | |
return NO; | |
} | |
return YES; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment