Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.43 KB | None | 0 0
  1. -(BOOL)isEmoji:(NSString *)character {
  2.  
  3. UILabel *characterRender = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
  4. characterRender.text = character;
  5. characterRender.backgroundColor = [UIColor blackColor];//needed to remove subpixel rendering colors
  6. [characterRender sizeToFit];
  7.  
  8. CGRect rect = [characterRender bounds];
  9. UIGraphicsBeginImageContextWithOptions(rect.size,YES,0.0f);
  10. CGContextRef contextSnap = UIGraphicsGetCurrentContext();
  11. [characterRender.layer renderInContext:contextSnap];
  12. UIImage *capturedImage = UIGraphicsGetImageFromCurrentImageContext();
  13. UIGraphicsEndImageContext();
  14.  
  15. CGImageRef imageRef = [capturedImage CGImage];
  16. NSUInteger width = CGImageGetWidth(imageRef);
  17. NSUInteger height = CGImageGetHeight(imageRef);
  18. CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
  19. unsigned char *rawData = (unsigned char*) calloc(height * width * 4, sizeof(unsigned char));
  20. NSUInteger bytesPerPixel = 4;
  21. NSUInteger bytesPerRow = bytesPerPixel * width;
  22. NSUInteger bitsPerComponent = 8;
  23. CGContextRef context = CGBitmapContextCreate(rawData, width, height,
  24. bitsPerComponent, bytesPerRow, colorSpace,
  25. kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
  26. CGColorSpaceRelease(colorSpace);
  27.  
  28. CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
  29. CGContextRelease(context);
  30.  
  31. BOOL colorPixelFound = NO;
  32.  
  33. int x = 0;
  34. int y = 0;
  35. while (y < height && !colorPixelFound) {
  36. while (x < width && !colorPixelFound) {
  37.  
  38. NSUInteger byteIndex = (bytesPerRow * y) + x * bytesPerPixel;
  39.  
  40. CGFloat red = (CGFloat)rawData[byteIndex];
  41. CGFloat green = (CGFloat)rawData[byteIndex+1];
  42. CGFloat blue = (CGFloat)rawData[byteIndex+2];
  43.  
  44. CGFloat h, s, b, a;
  45. UIColor *c = [UIColor colorWithRed:red green:green blue:blue alpha:1.0f];
  46. [c getHue:&h saturation:&s brightness:&b alpha:&a];
  47.  
  48. b /= 255.0f;
  49.  
  50. if (b > 0) {
  51. colorPixelFound = YES;
  52. }
  53.  
  54. x++;
  55. }
  56. x=0;
  57. y++;
  58. }
  59.  
  60. return colorPixelFound;
  61.  
  62. }
  63.  
  64. - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
  65. {
  66. // Detect if an Emoji is in the string "text"
  67. if(text.isIncludingEmoji) {
  68. // Show an UIAlertView, or whatever you want here
  69. return NO;
  70. }
  71.  
  72. return YES;
  73. }
  74.  
  75. unichar unicodevalue = [text characterAtIndex:0];
  76.  
  77. - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
  78.  
  79. // Get the UTF-16 representation of the text.
  80. unsigned long length = text.length;
  81. unichar buffer[length];
  82. [text getCharacters:buffer];
  83.  
  84. // Initialize array to hold our UTF-32 values.
  85. NSMutableArray *array = [[NSMutableArray alloc] init];
  86.  
  87. // Temporary stores for the UTF-32 and UTF-16 values.
  88. UTF32Char utf32 = 0;
  89. UTF16Char h16 = 0, l16 = 0;
  90.  
  91. for (int i = 0; i < length; i++) {
  92. unichar surrogate = buffer[i];
  93.  
  94. // High surrogate.
  95. if (0xd800 <= surrogate && surrogate <= 0xd83f) {
  96. h16 = surrogate;
  97. continue;
  98. }
  99. // Low surrogate.
  100. else if (0xdc00 <= surrogate && surrogate <= 0xdfff) {
  101. l16 = surrogate;
  102.  
  103. // Convert surrogate pair to UTF-32 encoding.
  104. utf32 = ((h16 - 0xd800) << 10) + (l16 - 0xdc00) + 0x10000;
  105. }
  106. // Normal UTF-16.
  107. else {
  108. utf32 = surrogate;
  109. }
  110.  
  111. // Add UTF-32 value to array.
  112. [array addObject:[NSNumber numberWithUnsignedInteger:utf32]];
  113. }
  114.  
  115. NSLog(@"%@ contains values:", text);
  116.  
  117. for (int i = 0; i < array.count; i++) {
  118. UTF32Char character = (UTF32Char)[[array objectAtIndex:i] unsignedIntegerValue];
  119. NSLog(@"t- U+%x", character);
  120. }
  121.  
  122. return YES;
  123. }
  124.  
  125. 😎 contains values:
  126. - U+1f60e
  127.  
  128. UILabel *characterRender = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 1, 1)];
  129. characterRender.text = character;
  130. characterRender.backgroundColor = [UIColor blackColor];//needed to remove subpixel rendering colors
  131. [characterRender sizeToFit];
  132.  
  133. CGRect rect = [characterRender bounds];
  134. UIGraphicsBeginImageContextWithOptions(rect.size,YES,0.0f);
  135. CGContextRef contextSnap = UIGraphicsGetCurrentContext();
  136. [characterRender.layer renderInContext:contextSnap];
  137. UIImage *capturedImage = UIGraphicsGetImageFromCurrentImageContext();
  138. UIGraphicsEndImageContext();
  139.  
  140. CGImageRef imageRef = [capturedImage CGImage];
  141. NSUInteger width = CGImageGetWidth(imageRef);
  142. NSUInteger height = CGImageGetHeight(imageRef);
  143. CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
  144. unsigned char *rawData = (unsigned char*) malloc(height * width * 4 * sizeof(unsigned char));
  145. NSUInteger bytesPerPixel = 4;
  146. NSUInteger bytesPerRow = bytesPerPixel * width;
  147. NSUInteger bitsPerComponent = 8;
  148. CGContextRef context = CGBitmapContextCreate(rawData, width, height,
  149. bitsPerComponent, bytesPerRow, colorSpace,
  150. kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
  151. CGColorSpaceRelease(colorSpace);
  152.  
  153. CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
  154. CGContextRelease(context);
  155.  
  156. BOOL colorPixelFound = NO;
  157.  
  158. int x = 0;
  159. int y = 0;
  160. while (y < height && !colorPixelFound) {
  161. while (x < width && !colorPixelFound) {
  162.  
  163. NSUInteger byteIndex = (bytesPerRow * y) + x * bytesPerPixel;
  164.  
  165. CGFloat red = (CGFloat)rawData[byteIndex];
  166. CGFloat green = (CGFloat)rawData[byteIndex+1];
  167. CGFloat blue = (CGFloat)rawData[byteIndex+2];
  168.  
  169. CGFloat h, s, b, a;
  170. UIColor *c = [UIColor colorWithRed:red green:green blue:blue alpha:1.0f];
  171. [c getHue:&h saturation:&s brightness:&b alpha:&a];
  172.  
  173. b /= 255.0f;
  174.  
  175. if (b > 0) {
  176. colorPixelFound = YES;
  177. }
  178.  
  179. x++;
  180. }
  181. x=0;
  182. y++;
  183. }
  184.  
  185. free(rawData);
  186. return colorPixelFound;
  187.  
  188. [myString canBeConvertedToEncoding:NSASCIIStringEncoding];
  189.  
  190. - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
  191.  
  192. {
  193.  
  194. // Detect if an Emoji is in the string "text"
  195. if([text length]==2) {
  196. // Show an UIAlertView, or whatever you want here
  197. return YES;
  198. }
  199. else
  200. {
  201.  
  202. return NO;
  203. }
  204.  
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement