画一个如”钉钉”软件中带名字的头像
1.效果图
2.主要代码
+ (UIImage *)circleImageWithText:(NSString *)text bgColor:(UIColor *)bgColor size:(CGSize)size{
NSDictionary *fontAttributes = @{NSFontAttributeName: [UIFont systemFontOfSize:30], NSForegroundColorAttributeName: [UIColor whiteColor]};
CGSize textSize = [text sizeWithAttributes:fontAttributes];
CGPoint drawPoint = CGPointMake((size.width - textSize.width)/2, (size.height - textSize.height)/2);
UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
CGContextRef ctx = UIGraphicsGetCurrentContext();
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, size.width, size.height)];
CGContextSetFillColorWithColor(ctx, bgColor.CGColor);
[path fill];
[text drawAtPoint:drawPoint withAttributes:fontAttributes];
UIImage *resultImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resultImg;
}
3.demo地址:
https://github.com/LGLee/dingdingIcon.git