首页 > 系统 > iOS > 正文

iOS-画一个"钉钉"圆形带名字的头像

2019-11-06 09:40:51
字体:
来源:转载
供稿:网友

画一个如”钉钉”软件中带名字的头像

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


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表