1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
+ (UIImage *)zd_imageWithColor:(UIColor *)color size:(CGSize)size text:(NSString *)text textAttributes:(NSDictionary *)textAttributes circular:(BOOL)isCircular { if (!color || size.width <= 0 || size.height <= 0) return nil; CGRect rect = CGRectMake(0, 0, size.width, size.height); UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0); CGContextRef context = UIGraphicsGetCurrentContext(); if (isCircular) { CGPathRef path = CGPathCreateWithEllipseInRect(rect, NULL); CGContextAddPath(context, path); CGContextClip(context); CGPathRelease(path); } CGContextSetFillColorWithColor(context, color.CGColor); CGContextFillRect(context, rect); CGSize textSize = [text sizeWithAttributes:textAttributes]; [text drawInRect:CGRectMake((size.width - textSize.width) / 2, (size.height - textSize.height) / 2, textSize.width, textSize.height) withAttributes:textAttributes]; UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image; }
|