Advertisement
Guest User

Untitled

a guest
Feb 11th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.12 KB | None | 0 0
  1. CGContextRef context = UIGraphicsGetCurrentContext();
  2.  
  3. CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);
  4. CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
  5. CGContextSetLineWidth(context, 1.0);
  6. CGContextSelectFont(context, "Helvetica", 20, kCGEncodingMacRoman);
  7. CGContextSetTextDrawingMode(context, kCGTextFill);
  8.  
  9. CGPoint center = self.center;
  10.  
  11. UIFont* font = [UIFont fontWithName:@"Geneva" size:12.0];
  12. [@"Some text" drawAtPoint:center withFont:font];
  13.  
  14. CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);
  15. CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
  16.  
  17. NSString *text = @"Some text";
  18. CGPoint center = CGPointMake(100, 200);
  19. UIFont *font = [UIFont systemFontOfSize:14];
  20.  
  21. CGSize stringSize = [text sizeWithFont:font];
  22. CGRect stringRect = CGRectMake(center.x-stringSize.width/2, center.y-stringSize.height/2, stringSize.width, stringSize.height);
  23.  
  24. [[UIColor blackColor] set];
  25. CGContextFillRect(context, stringRect);
  26.  
  27. [[UIColor whiteColor] set];
  28. [text drawInRect:stringRect withFont:font];
  29.  
  30. CGContextShowTextAtPoint (context, center.x, center.y, "Some text", strlen("Some text"));
  31.  
  32. -(UIImage *)addText:(UIImage *)img text:(NSString *)text
  33. {
  34. int w = img.size.width;
  35. int h = img.size.height;
  36. CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
  37. CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
  38.  
  39. CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);
  40. CGContextSetTextMatrix(context, CGAffineTransformIdentity);
  41.  
  42. // Create a path which bounds the area where you will be drawing text.
  43. // The path need not be rectangular.
  44. CGMutablePathRef path = CGPathCreateMutable();
  45.  
  46. float fontsize = 150;
  47. NSString *fontName = @"Helvetica";
  48. UIFont *ufont = [UIFont fontWithName:fontName size:fontsize];
  49. NSDictionary *textAttributes = @{NSFontAttributeName:ufont
  50. };
  51. CGSize stringSize = [text sizeWithAttributes:textAttributes];
  52. // In this simple example, initialize a rectangular path.
  53. CGRect bounds = CGRectMake(10.0, 10.0, stringSize.width+20, stringSize.height+50);
  54.  
  55. CGContextSetFillColorWithColor(context, [UIColor colorWithRed:1 green:1 blue:1 alpha:.3].CGColor);
  56. CGContextFillRect(context, bounds);
  57.  
  58. CGPathAddRect(path, NULL, bounds );
  59.  
  60. // Initialize a string.
  61. CFStringRef textString = (__bridge CFStringRef)(text);
  62.  
  63. // Create a mutable attributed string with a max length of 0.
  64. // The max length is a hint as to how much internal storage to reserve.
  65. // 0 means no hint.
  66. CFMutableAttributedStringRef attrString =
  67. CFAttributedStringCreateMutable(kCFAllocatorDefault, 0);
  68.  
  69. // Copy the textString into the newly created attrString
  70. CFAttributedStringReplaceString (attrString, CFRangeMake(0, 0),
  71. textString);
  72.  
  73. // Create a color that will be added as an attribute to the attrString.
  74. CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
  75. CGFloat components[] = { 1.0, 0.0, 0.0, 0.8 };
  76. CGColorRef red = CGColorCreate(rgbColorSpace, components);
  77. CGColorSpaceRelease(rgbColorSpace);
  78.  
  79. CTFontRef font = CTFontCreateWithName((CFStringRef)fontName, fontsize, nil);
  80. CFAttributedStringSetAttribute(attrString,CFRangeMake(0, [text length]),kCTFontAttributeName,font);
  81.  
  82. // Set the color of the first 12 chars to red.
  83. CFAttributedStringSetAttribute(attrString, CFRangeMake(0, [text length]),
  84. kCTForegroundColorAttributeName, red);
  85.  
  86. // Create the framesetter with the attributed string.
  87. CTFramesetterRef framesetter =
  88. CTFramesetterCreateWithAttributedString(attrString);
  89. CFRelease(attrString);
  90.  
  91. // Create a frame.
  92. CTFrameRef frame = CTFramesetterCreateFrame(framesetter,
  93. CFRangeMake(0, 0), path, NULL);
  94.  
  95. // Draw the specified frame in the given context.
  96. CTFrameDraw(frame, context);
  97.  
  98. // Release the objects we used.
  99. CFRelease(frame);
  100. CFRelease(path);
  101. CFRelease(framesetter);
  102.  
  103. CFRelease(font);
  104. CGImageRef imageMasked = CGBitmapContextCreateImage(context);
  105.  
  106. CGContextRelease(context);
  107. CGColorSpaceRelease(colorSpace);
  108.  
  109. UIImage *result = [UIImage imageWithCGImage:imageMasked];
  110. CGImageRelease(imageMasked);
  111. return result;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement