iOS-畫一個"釘釘"圓形帶名字的頭像

畫一個如”釘釘”軟件中帶名字的頭像

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

發佈了72 篇原創文章 · 獲贊 13 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章