Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- CGContextRef context = UIGraphicsGetCurrentContext();
- CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);
- CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
- CGContextSetLineWidth(context, 1.0);
- CGContextSelectFont(context, "Helvetica", 20, kCGEncodingMacRoman);
- CGContextSetTextDrawingMode(context, kCGTextFill);
- CGPoint center = self.center;
- UIFont* font = [UIFont fontWithName:@"Geneva" size:12.0];
- [@"Some text" drawAtPoint:center withFont:font];
- CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);
- CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
- NSString *text = @"Some text";
- CGPoint center = CGPointMake(100, 200);
- UIFont *font = [UIFont systemFontOfSize:14];
- CGSize stringSize = [text sizeWithFont:font];
- CGRect stringRect = CGRectMake(center.x-stringSize.width/2, center.y-stringSize.height/2, stringSize.width, stringSize.height);
- [[UIColor blackColor] set];
- CGContextFillRect(context, stringRect);
- [[UIColor whiteColor] set];
- [text drawInRect:stringRect withFont:font];
- CGContextShowTextAtPoint (context, center.x, center.y, "Some text", strlen("Some text"));
- -(UIImage *)addText:(UIImage *)img text:(NSString *)text
- {
- int w = img.size.width;
- int h = img.size.height;
- CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
- CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
- CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);
- CGContextSetTextMatrix(context, CGAffineTransformIdentity);
- // Create a path which bounds the area where you will be drawing text.
- // The path need not be rectangular.
- CGMutablePathRef path = CGPathCreateMutable();
- float fontsize = 150;
- NSString *fontName = @"Helvetica";
- UIFont *ufont = [UIFont fontWithName:fontName size:fontsize];
- NSDictionary *textAttributes = @{NSFontAttributeName:ufont
- };
- CGSize stringSize = [text sizeWithAttributes:textAttributes];
- // In this simple example, initialize a rectangular path.
- CGRect bounds = CGRectMake(10.0, 10.0, stringSize.width+20, stringSize.height+50);
- CGContextSetFillColorWithColor(context, [UIColor colorWithRed:1 green:1 blue:1 alpha:.3].CGColor);
- CGContextFillRect(context, bounds);
- CGPathAddRect(path, NULL, bounds );
- // Initialize a string.
- CFStringRef textString = (__bridge CFStringRef)(text);
- // Create a mutable attributed string with a max length of 0.
- // The max length is a hint as to how much internal storage to reserve.
- // 0 means no hint.
- CFMutableAttributedStringRef attrString =
- CFAttributedStringCreateMutable(kCFAllocatorDefault, 0);
- // Copy the textString into the newly created attrString
- CFAttributedStringReplaceString (attrString, CFRangeMake(0, 0),
- textString);
- // Create a color that will be added as an attribute to the attrString.
- CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
- CGFloat components[] = { 1.0, 0.0, 0.0, 0.8 };
- CGColorRef red = CGColorCreate(rgbColorSpace, components);
- CGColorSpaceRelease(rgbColorSpace);
- CTFontRef font = CTFontCreateWithName((CFStringRef)fontName, fontsize, nil);
- CFAttributedStringSetAttribute(attrString,CFRangeMake(0, [text length]),kCTFontAttributeName,font);
- // Set the color of the first 12 chars to red.
- CFAttributedStringSetAttribute(attrString, CFRangeMake(0, [text length]),
- kCTForegroundColorAttributeName, red);
- // Create the framesetter with the attributed string.
- CTFramesetterRef framesetter =
- CTFramesetterCreateWithAttributedString(attrString);
- CFRelease(attrString);
- // Create a frame.
- CTFrameRef frame = CTFramesetterCreateFrame(framesetter,
- CFRangeMake(0, 0), path, NULL);
- // Draw the specified frame in the given context.
- CTFrameDraw(frame, context);
- // Release the objects we used.
- CFRelease(frame);
- CFRelease(path);
- CFRelease(framesetter);
- CFRelease(font);
- CGImageRef imageMasked = CGBitmapContextCreateImage(context);
- CGContextRelease(context);
- CGColorSpaceRelease(colorSpace);
- UIImage *result = [UIImage imageWithCGImage:imageMasked];
- CGImageRelease(imageMasked);
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement