Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -(BOOL)isEmoji:(NSString *)character {
- UILabel *characterRender = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
- characterRender.text = character;
- characterRender.backgroundColor = [UIColor blackColor];//needed to remove subpixel rendering colors
- [characterRender sizeToFit];
- CGRect rect = [characterRender bounds];
- UIGraphicsBeginImageContextWithOptions(rect.size,YES,0.0f);
- CGContextRef contextSnap = UIGraphicsGetCurrentContext();
- [characterRender.layer renderInContext:contextSnap];
- UIImage *capturedImage = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- CGImageRef imageRef = [capturedImage CGImage];
- NSUInteger width = CGImageGetWidth(imageRef);
- NSUInteger height = CGImageGetHeight(imageRef);
- CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
- unsigned char *rawData = (unsigned char*) calloc(height * width * 4, sizeof(unsigned char));
- NSUInteger bytesPerPixel = 4;
- NSUInteger bytesPerRow = bytesPerPixel * width;
- NSUInteger bitsPerComponent = 8;
- CGContextRef context = CGBitmapContextCreate(rawData, width, height,
- bitsPerComponent, bytesPerRow, colorSpace,
- kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
- CGColorSpaceRelease(colorSpace);
- CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
- CGContextRelease(context);
- BOOL colorPixelFound = NO;
- int x = 0;
- int y = 0;
- while (y < height && !colorPixelFound) {
- while (x < width && !colorPixelFound) {
- NSUInteger byteIndex = (bytesPerRow * y) + x * bytesPerPixel;
- CGFloat red = (CGFloat)rawData[byteIndex];
- CGFloat green = (CGFloat)rawData[byteIndex+1];
- CGFloat blue = (CGFloat)rawData[byteIndex+2];
- CGFloat h, s, b, a;
- UIColor *c = [UIColor colorWithRed:red green:green blue:blue alpha:1.0f];
- [c getHue:&h saturation:&s brightness:&b alpha:&a];
- b /= 255.0f;
- if (b > 0) {
- colorPixelFound = YES;
- }
- x++;
- }
- x=0;
- y++;
- }
- return colorPixelFound;
- }
- - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
- {
- // Detect if an Emoji is in the string "text"
- if(text.isIncludingEmoji) {
- // Show an UIAlertView, or whatever you want here
- return NO;
- }
- return YES;
- }
- unichar unicodevalue = [text characterAtIndex:0];
- - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
- // Get the UTF-16 representation of the text.
- unsigned long length = text.length;
- unichar buffer[length];
- [text getCharacters:buffer];
- // Initialize array to hold our UTF-32 values.
- NSMutableArray *array = [[NSMutableArray alloc] init];
- // Temporary stores for the UTF-32 and UTF-16 values.
- UTF32Char utf32 = 0;
- UTF16Char h16 = 0, l16 = 0;
- for (int i = 0; i < length; i++) {
- unichar surrogate = buffer[i];
- // High surrogate.
- if (0xd800 <= surrogate && surrogate <= 0xd83f) {
- h16 = surrogate;
- continue;
- }
- // Low surrogate.
- else if (0xdc00 <= surrogate && surrogate <= 0xdfff) {
- l16 = surrogate;
- // Convert surrogate pair to UTF-32 encoding.
- utf32 = ((h16 - 0xd800) << 10) + (l16 - 0xdc00) + 0x10000;
- }
- // Normal UTF-16.
- else {
- utf32 = surrogate;
- }
- // Add UTF-32 value to array.
- [array addObject:[NSNumber numberWithUnsignedInteger:utf32]];
- }
- NSLog(@"%@ contains values:", text);
- for (int i = 0; i < array.count; i++) {
- UTF32Char character = (UTF32Char)[[array objectAtIndex:i] unsignedIntegerValue];
- NSLog(@"t- U+%x", character);
- }
- return YES;
- }
- 😎 contains values:
- - U+1f60e
- UILabel *characterRender = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
- characterRender.text = character;
- characterRender.backgroundColor = [UIColor blackColor];//needed to remove subpixel rendering colors
- [characterRender sizeToFit];
- CGRect rect = [characterRender bounds];
- UIGraphicsBeginImageContextWithOptions(rect.size,YES,0.0f);
- CGContextRef contextSnap = UIGraphicsGetCurrentContext();
- [characterRender.layer renderInContext:contextSnap];
- UIImage *capturedImage = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- CGImageRef imageRef = [capturedImage CGImage];
- NSUInteger width = CGImageGetWidth(imageRef);
- NSUInteger height = CGImageGetHeight(imageRef);
- CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
- unsigned char *rawData = (unsigned char*) malloc(height * width * 4 * sizeof(unsigned char));
- NSUInteger bytesPerPixel = 4;
- NSUInteger bytesPerRow = bytesPerPixel * width;
- NSUInteger bitsPerComponent = 8;
- CGContextRef context = CGBitmapContextCreate(rawData, width, height,
- bitsPerComponent, bytesPerRow, colorSpace,
- kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
- CGColorSpaceRelease(colorSpace);
- CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
- CGContextRelease(context);
- BOOL colorPixelFound = NO;
- int x = 0;
- int y = 0;
- while (y < height && !colorPixelFound) {
- while (x < width && !colorPixelFound) {
- NSUInteger byteIndex = (bytesPerRow * y) + x * bytesPerPixel;
- CGFloat red = (CGFloat)rawData[byteIndex];
- CGFloat green = (CGFloat)rawData[byteIndex+1];
- CGFloat blue = (CGFloat)rawData[byteIndex+2];
- CGFloat h, s, b, a;
- UIColor *c = [UIColor colorWithRed:red green:green blue:blue alpha:1.0f];
- [c getHue:&h saturation:&s brightness:&b alpha:&a];
- b /= 255.0f;
- if (b > 0) {
- colorPixelFound = YES;
- }
- x++;
- }
- x=0;
- y++;
- }
- free(rawData);
- return colorPixelFound;
- [myString canBeConvertedToEncoding:NSASCIIStringEncoding];
- - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
- {
- // Detect if an Emoji is in the string "text"
- if([text length]==2) {
- // Show an UIAlertView, or whatever you want here
- return YES;
- }
- else
- {
- return NO;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement