iOS 圖片文字組合頭像那點(diǎn)事

碧玉妝成一樹高,萬(wàn)條垂下綠絲絳。
不知細(xì)葉誰(shuí)裁出,二月春風(fēng)似剪刀

前言

歲月無(wú)情,轉(zhuǎn)眼間年已過(guò)完,本打算在年前寫一篇視頻編輯處理方面的文章,奈何時(shí)間...舊事未完,新事又來(lái),在最近的項(xiàng)目中,需要實(shí)現(xiàn)如下圖中的效果


xiaoguo.png

由于時(shí)間緊迫,本打算找個(gè)現(xiàn)成的..居然沒(méi)找到,好吧!那就自己動(dòng)手,豐衣足食吧。

思路

由于服務(wù)器只返回圖片或者人員名字,所以想全部通過(guò)圖片來(lái)拼接的方式不可行,為了性能,就采取了Quartz 2DCGContextRef的方式來(lái)進(jìn)行繪制。最開始我的思路是先繪制一個(gè)大圓圈,然后再繪制幾條白色的線條,來(lái)實(shí)現(xiàn)目標(biāo),但是問(wèn)題來(lái)了,就是文字的背景顏色,于是放棄該方案,最終選擇通過(guò)返回的數(shù)據(jù)的個(gè)數(shù)來(lái)繪制不同的弧面(最多4個(gè)),在繪制弧面的時(shí)候,讓弧度之間的空白處作為白色線條。

first

在思路確定好后,就準(zhǔn)備開始動(dòng)手進(jìn)行敲鍵盤了,為了先試試效果,先寫了一個(gè)純文字來(lái)繪制圖片的API,如下

+ (UIImage *)gl_creatImageWithString:(NSAttributedString *)string imageSize:(CGSize)imageSize imageColor:(UIColor *)imageColor
{
    //通過(guò)自己創(chuàng)建一個(gè)context來(lái)繪制,通常用于對(duì)圖片的處理
    UIGraphicsBeginImageContextWithOptions(imageSize, NO, [UIScreen mainScreen].scale);
    //獲取上下文
    CGContextRef context = UIGraphicsGetCurrentContext();
    //設(shè)置填充顏色
    CGContextSetFillColorWithColor(context, imageColor.CGColor);
    //直接按rect的范圍覆蓋
    CGContextAddEllipseInRect(context, CGRectMake(0, 0, imageSize.width, imageSize.height));
    CGContextFillPath(context);

    CGSize stringSize = [string size];
    CGFloat x = (imageSize.width - stringSize.width)/2.0;
    CGFloat y = (imageSize.height - stringSize.height)/2.0;
    
    [string drawInRect:CGRectMake(x, y, stringSize.width, stringSize.height)];
    
    UIImage *newimg = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newimg;
}

來(lái),看看效果

wenzi.jpeg

恩!感覺(jué)不錯(cuò),心里一陣竊喜!這里入?yún)?code>NSAttributedString變量類型,方便自己設(shè)置大小顏色等效果

second

繪制圓形圖片,這個(gè)在之前的文章中已經(jīng)實(shí)現(xiàn)該功能,所以就節(jié)省了步驟,代碼如下

+ (UIImage *)gl_circleImage:(UIImage *)image
{
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(image.size.width, image.size.height), NO, [UIScreen mainScreen].scale);
    //獲取上下文
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height);

    //畫橢圓 當(dāng)寬和高一樣的時(shí)候 為圓
    CGContextAddEllipseInRect(context, rect);
    //剪切可視范圍
    CGContextClip(context);
    
    //繪制圖片
    [image drawInRect:rect];
    
    UIImage *newimg = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newimg;
}

懷抱激動(dòng)的心情,于是寫下了如下代碼

            //通過(guò)自己創(chuàng)建一個(gè)context來(lái)繪制,通常用于對(duì)圖片的處理
            UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);
            //獲取上下文
            CGContextRef context = UIGraphicsGetCurrentContext();


            CGFloat radius = size.width/2.0 - 1;
            CGPoint center = CGPointMake(size.width/2.0 - 1, size.height/2.0);
            UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:M_PI_2 endAngle:M_PI +  M_PI_2 clockwise:YES];
            [path addLineToPoint:center];
            
            CGPoint center_n = CGPointMake(size.width/2.0 + 1, size.height/2.0);
            UIBezierPath *path_n = [UIBezierPath bezierPathWithArcCenter:center_n radius:radius startAngle:M_PI + M_PI_2 endAngle:2 * M_PI + M_PI_2 clockwise:YES];
            [path_n addLineToPoint:center_n];
            
            
            CGContextAddPath(context, path_n.CGPath);
            
            CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
            CGContextAddPath(context, path.CGPath);
            
            CGContextFillPath(context);
            
            NSAttributedString *string = (NSAttributedString*)contents[0];
            CGSize stringSize = [string size];
            CGFloat x = (path.bounds.size.width - stringSize.width)/2.0;
            CGFloat y = (path.bounds.size.height - stringSize.height)/2.0;
            
            [string drawInRect:CGRectMake(x, y, stringSize.width, stringSize.height)];

            
            CGContextClip(context);

            if ([contents[1] isKindOfClass:[UIImage class]]) {
                UIImage *img = (UIImage *)contents[1];
                [img drawInRect:path_n.bounds];
            }
            UIImage *newimg = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
            return newimg;

然而在運(yùn)行的那一刻,瞬間淚崩

Simulator Screen Shot - iPhone X - 2018-03-12 at 16.39.34.png

什么鬼,不是說(shuō)好的半圓么....這個(gè)問(wèn)題讓我真是苦惱,仔細(xì)檢查代碼,發(fā)現(xiàn)沒(méi)啥問(wèn)題呀,路徑也是半圓的路徑,正在一籌莫展的時(shí)候,突然在網(wǎng)上看到一個(gè)問(wèn)題CGContextStrokePath, CGContextFillPath, CGContextEOFillPath, CGContext- DrawPath. 描邊或者填充操作都會(huì)清除這個(gè)路徑,清除路徑,也就是說(shuō)當(dāng)我執(zhí)行CGContextClip的時(shí)候,路徑已經(jīng)不在了,所以我想要的半圓也就沒(méi)了??吹竭@,我立刻改了下代碼,如下

            //通過(guò)自己創(chuàng)建一個(gè)context來(lái)繪制,通常用于對(duì)圖片的處理
            UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);
            //獲取上下文
            CGContextRef context = UIGraphicsGetCurrentContext();


            CGFloat radius = size.width/2.0 - 1;
            CGPoint center = CGPointMake(size.width/2.0 - 1, size.height/2.0);
            UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:M_PI_2 endAngle:M_PI +  M_PI_2 clockwise:YES];
            [path addLineToPoint:center];
            
            CGPoint center_n = CGPointMake(size.width/2.0 + 1, size.height/2.0);
            UIBezierPath *path_n = [UIBezierPath bezierPathWithArcCenter:center_n radius:radius startAngle:M_PI + M_PI_2 endAngle:2 * M_PI + M_PI_2 clockwise:YES];
            [path_n addLineToPoint:center_n];
            
            
            CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
            CGContextAddPath(context, path.CGPath);
            
            CGContextFillPath(context);
            
            NSAttributedString *string = (NSAttributedString*)contents[0];
            CGSize stringSize = [string size];
            CGFloat x = (path.bounds.size.width - stringSize.width)/2.0;
            CGFloat y = (path.bounds.size.height - stringSize.height)/2.0;
            
            [string drawInRect:CGRectMake(x, y, stringSize.width, stringSize.height)];

            CGContextAddPath(context, path_n.CGPath);
            
            CGContextClip(context);

            if ([contents[1] isKindOfClass:[UIImage class]]) {
                UIImage *img = (UIImage *)contents[1];
                [img drawInRect:path_n.bounds];
            }
            UIImage *newimg = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
            return newimg;

CGContextAddPath(context, path_n.CGPath);添加半圓的路徑移動(dòng)到CGContextFillPath(context);后面CGContextClip(context);前面,再一執(zhí)行,瞬間效果就對(duì)了

Simulator Screen Shot - iPhone X - 2018-03-12 at 16.39.54.png

last

在上面的問(wèn)題都解決好之后,剩余的問(wèn)題就都不是問(wèn)題了,就是一些簡(jiǎn)單邏輯和坐標(biāo)的計(jì)算,代碼如下

+ (UIImage *)gl_groupHeadPortraitWithContents:(NSArray *)contents size:(CGSize)size
{
    NSAssert(contents.count <=4 && contents.count >0, @"contents can not be empty array");
    switch (contents.count) {
        case 1:
        {
            if ([contents[0] isKindOfClass:[UIImage class]]) {
                return [self gl_circleImage:(UIImage *)contents[0]];
            }else if ([contents[0] isKindOfClass:[NSAttributedString class]]){
                return [self gl_creatImageWithString:contents[0] imageSize:size imageColor:[UIColor grayColor]];
            }
        }
            break;
        case 2:
        {
            //通過(guò)自己創(chuàng)建一個(gè)context來(lái)繪制,通常用于對(duì)圖片的處理
            UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);
            //獲取上下文
            CGContextRef context = UIGraphicsGetCurrentContext();


            CGFloat radius = size.width/2.0 - 1;
            CGPoint center = CGPointMake(size.width/2.0 - 1, size.height/2.0);
            UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:M_PI_2 endAngle:M_PI +  M_PI_2 clockwise:YES];
            [path addLineToPoint:center];
            
            CGPoint center_n = CGPointMake(size.width/2.0 + 1, size.height/2.0);
            UIBezierPath *path_n = [UIBezierPath bezierPathWithArcCenter:center_n radius:radius startAngle:M_PI + M_PI_2 endAngle:2 * M_PI + M_PI_2 clockwise:YES];
            [path_n addLineToPoint:center_n];
            
            if ([contents[0] isKindOfClass:[UIImage class]]) {
                CGContextAddPath(context, path.CGPath);
                
            }else{
                CGContextSetFillColorWithColor(context, [UIColor redColor].CGColor);
                CGContextAddPath(context, path.CGPath);
                
                CGContextFillPath(context);
                
                NSAttributedString *string = (NSAttributedString*)contents[0];
                CGSize stringSize = [string size];
                CGFloat x = (path.bounds.size.width - stringSize.width)/2.0;
                CGFloat y = (path.bounds.size.height - stringSize.height)/2.0;
                
                [string drawInRect:CGRectMake(x, y, stringSize.width, stringSize.height)];
            }
            
            if ([contents[1] isKindOfClass:[UIImage class]]) {
                CGContextAddPath(context, path_n.CGPath);
            }else{
                CGContextSetFillColorWithColor(context, [UIColor orangeColor].CGColor);
                CGContextAddPath(context, path_n.CGPath);
                
                CGContextFillPath(context);
                
                NSAttributedString *string = (NSAttributedString*)contents[1];
                CGSize stringSize = [string size];
                CGFloat x = path_n.currentPoint.x + (path_n.bounds.size.width - stringSize.width)/2.0;
                CGFloat y = (path_n.bounds.size.height - stringSize.height)/2.0;
                
                [string drawInRect:CGRectMake(x, y, stringSize.width, stringSize.height)];
                
                //圖片在前的時(shí)候 文字在后 路徑被清了 導(dǎo)致后面的clip剪切不了路徑
                CGContextAddPath(context, path.CGPath);
            }
            
            CGContextClip(context);
            
            if ([contents[0] isKindOfClass:[UIImage class]]) {
                UIImage *img = (UIImage *)contents[0];
                [img drawInRect:path.bounds];
            }
            if ([contents[1] isKindOfClass:[UIImage class]]) {
                UIImage *img = (UIImage *)contents[1];
                [img drawInRect:path_n.bounds];
            }
            UIImage *newimg = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
            return newimg;
        }
            break;
        case 3:
        {
            //通過(guò)自己創(chuàng)建一個(gè)context來(lái)繪制,通常用于對(duì)圖片的處理
            UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);
            //獲取上下文
            CGContextRef context = UIGraphicsGetCurrentContext();
            
            //先確定三個(gè)路徑
            CGFloat radius = size.width/2.0 - 1;
            CGPoint center = CGPointMake(size.width/2.0 - 1, size.height/2.0);
            UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:M_PI_2 endAngle:M_PI +  M_PI_2 clockwise:YES];
            [path addLineToPoint:center];
            
            CGPoint center_s = CGPointMake(size.width/2.0 + 1, size.height/2.0 - 1);
            UIBezierPath *path_s = [UIBezierPath bezierPathWithArcCenter:center_s radius:radius-1 startAngle:M_PI + M_PI_2 endAngle:2 * M_PI clockwise:YES];
            [path_s addLineToPoint:center_s];
            
            
            CGPoint center_t = CGPointMake(size.width/2.0 + 1, size.height/2.0 + 1);
            UIBezierPath *path_t = [UIBezierPath bezierPathWithArcCenter:center_t radius:radius-1 startAngle:2 * M_PI endAngle:2 * M_PI + M_PI_2 clockwise:YES];
            [path_t addLineToPoint:center_t];
            
            NSMutableArray *imageArray = [[NSMutableArray alloc] init];
            NSMutableArray *titleArray = [[NSMutableArray alloc] init];
            for (id object in contents) {
                if ([object isKindOfClass:[UIImage class]]) {
                    [imageArray addObject:object];
                }else{
                    [titleArray addObject:object];
                }
            }
            
            if (imageArray.count == 3) {
                //全部為圖片
                CGContextAddPath(context, path.CGPath);
                CGContextAddPath(context, path_s.CGPath);
                CGContextAddPath(context, path_t.CGPath);
                
                CGContextClip(context);
 
                UIImage *img0 = (UIImage *)imageArray[0];
                UIImage *img1 = (UIImage *)imageArray[1];
                UIImage *img2 = (UIImage *)imageArray[2];
                
                [img0 drawInRect:path.bounds];
                [img1 drawInRect:path_s.bounds];
                [img2 drawInRect:path_t.bounds];
            }else if (imageArray.count == 2){
                //文字1
                CGContextSetFillColorWithColor(context, [UIColor orangeColor].CGColor);
                CGContextAddPath(context, path_t.CGPath);
                CGContextFillPath(context);
                
                NSAttributedString *string = (NSAttributedString*)titleArray[0];
                CGSize stringSize = [string size];
                CGFloat x = path_t.currentPoint.x + (path_t.bounds.size.width - stringSize.width)/2.0;
                CGFloat y = path_t.currentPoint.y + (path_t.bounds.size.height - stringSize.height)/2.0;
                
                [string drawInRect:CGRectMake(x, y, stringSize.width, stringSize.height)];
                
                //圖片1 圖片2
                CGContextAddPath(context, path.CGPath);
                CGContextAddPath(context, path_s.CGPath);
                CGContextClip(context);
                
                UIImage *img0 = (UIImage *)imageArray[0];
                UIImage *img1 = (UIImage *)imageArray[1];
                [img0 drawInRect:path.bounds];
                [img1 drawInRect:path_s.bounds];

            }else if (imageArray.count == 1){
                
                //文字1
                CGContextSetFillColorWithColor(context, [UIColor orangeColor].CGColor);
                CGContextAddPath(context, path_t.CGPath);
                CGContextFillPath(context);
                
                NSAttributedString *string = (NSAttributedString*)titleArray[0];
                CGSize stringSize = [string size];
                CGFloat x = path_t.currentPoint.x + (path_t.bounds.size.width - stringSize.width)/2.0;
                CGFloat y = path_t.currentPoint.y + (path_t.bounds.size.height - stringSize.height)/2.0;
                
                [string drawInRect:CGRectMake(x, y, stringSize.width, stringSize.height)];
                
                
                //文字2
                CGContextSetFillColorWithColor(context, [UIColor purpleColor].CGColor);
                CGContextAddPath(context, path_s.CGPath);
                CGContextFillPath(context);
                
                NSAttributedString *string_s = (NSAttributedString*)titleArray[1];
                CGSize stringSize_s = [string_s size];
                CGFloat x_s = path_s.currentPoint.x + (path_s.bounds.size.width - stringSize_s.width)/2.0;
                CGFloat y_s = (path_s.bounds.size.height - stringSize_s.height)/2.0;
                
                [string_s drawInRect:CGRectMake(x_s, y_s, stringSize_s.width, stringSize_s.height)];
                
                //圖片
                CGContextAddPath(context, path.CGPath);
                CGContextClip(context);
                UIImage *img0 = (UIImage *)imageArray[0];
                [img0 drawInRect:path.bounds];
                
            }else{
                for (int i = 0; i < titleArray.count; i ++) {
                    UIColor *color;
                    UIBezierPath *drawPath;
                    
                    NSAttributedString *string_s = (NSAttributedString*)titleArray[i];
                    CGSize stringSize_s = [string_s size];
                    CGFloat x_s = 0;
                    CGFloat y_s = 0;
                    if (i == 0) {
                        color = [UIColor greenColor];
                        drawPath = path;
                        x_s = (drawPath.bounds.size.width - stringSize_s.width)/2.0;
                        y_s = (drawPath.bounds.size.height - stringSize_s.height)/2.0;
                    }else if(i == 1){
                        color = [UIColor purpleColor];
                        drawPath = path_s;
                        x_s = drawPath.currentPoint.x + (drawPath.bounds.size.width - stringSize_s.width)/2.0;
                        y_s = (drawPath.bounds.size.height - stringSize_s.height)/2.0;
                    }else{
                        color = [UIColor redColor];
                        drawPath = path_t;
                        
                        x_s = drawPath.currentPoint.x + (drawPath.bounds.size.width - stringSize_s.width)/2.0;
                        y_s = drawPath.currentPoint.y + (drawPath.bounds.size.height - stringSize_s.height)/2.0;
                    }
                    
                    CGContextSetFillColorWithColor(context, color.CGColor);
                    CGContextAddPath(context, drawPath.CGPath);
                    CGContextFillPath(context);
                    
                
                    
                    [string_s drawInRect:CGRectMake(x_s, y_s, stringSize_s.width, stringSize_s.height)];
                }
            }
            
            UIImage *newimg = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
            return newimg;
        }
            break;
        case 4:
        {
            CGSize imageSize = size;
            //通過(guò)自己創(chuàng)建一個(gè)context來(lái)繪制,通常用于對(duì)圖片的處理
            UIGraphicsBeginImageContextWithOptions(imageSize, NO, [UIScreen mainScreen].scale);
            //獲取上下文
            CGContextRef context = UIGraphicsGetCurrentContext();
            
            CGFloat radius = size.width/2.0 - 1;
            CGPoint center = CGPointMake(imageSize.width/2.0 + 1, imageSize.height/2.0 + 1);
            
            //路徑
            UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(imageSize.width/2.0 - 1, imageSize.height/2.0 - 1) radius:radius startAngle:M_PI endAngle:M_PI + M_PI_2 clockwise:YES];
            [path addLineToPoint:CGPointMake(imageSize.width/2.0 - 1, imageSize.height/2.0 - 1)];
            
            //路徑1
            UIBezierPath *path1 = [UIBezierPath bezierPathWithArcCenter:CGPointMake(imageSize.width/2.0 + 1, imageSize.height/2.0 - 1) radius:radius startAngle:M_PI + M_PI_2 endAngle:2*M_PI clockwise:YES];
            [path1 addLineToPoint:CGPointMake(imageSize.width/2.0 + 1, imageSize.height/2.0 - 1)];
            
            //路徑2
            UIBezierPath *path2 = [UIBezierPath bezierPathWithArcCenter:center radius:radius startAngle:0 endAngle:M_PI_2 clockwise:YES];
            [path2 addLineToPoint:center];
            
            //路徑3
            UIBezierPath *path3 = [UIBezierPath bezierPathWithArcCenter:CGPointMake(center.x-2, center.y) radius:radius startAngle:M_PI_2 endAngle:M_PI clockwise:YES];
            [path3 addLineToPoint:CGPointMake(center.x-2, center.y)];

            

            


            
            NSMutableArray *imageArray = [[NSMutableArray alloc] init];
            NSMutableArray *titleArray = [[NSMutableArray alloc] init];
            for (id object in contents) {
                if ([object isKindOfClass:[UIImage class]]) {
                    [imageArray addObject:object];
                }else{
                    [titleArray addObject:object];
                }
            }
            
            if (imageArray.count == 4) {
                //圖片
                CGContextAddPath(context, path.CGPath);
                CGContextAddPath(context, path1.CGPath);
                CGContextAddPath(context, path2.CGPath);
                CGContextAddPath(context, path3.CGPath);
                CGContextClip(context);
                
                UIImage *img0 = (UIImage *)imageArray[0];
                UIImage *img1 = (UIImage *)imageArray[1];
                UIImage *img2 = (UIImage *)imageArray[2];
                UIImage *img3 = (UIImage *)imageArray[3];
                [img0 drawInRect:path.bounds];
                [img1 drawInRect:path1.bounds];
                [img2 drawInRect:path2.bounds];
                [img3 drawInRect:path3.bounds];
            }else if (imageArray.count == 3){
                for (int i = 0; i < titleArray.count; i ++) {
                    UIColor *color;
                    UIBezierPath *drawPath;
                    
                    NSAttributedString *string_s = (NSAttributedString*)titleArray[i];
                    CGSize stringSize_s = [string_s size];
                    CGFloat x_s = 0;
                    CGFloat y_s = 0;
                    if (i == 0) {
                        color = [UIColor redColor];
                        drawPath = path3;
                        
                        x_s = (drawPath.bounds.size.width - stringSize_s.width)/2.0;
                        y_s = drawPath.currentPoint.y + (drawPath.bounds.size.height - stringSize_s.height)/2.0;
                    }
                    CGContextSetFillColorWithColor(context, color.CGColor);
                    CGContextAddPath(context, drawPath.CGPath);
                    CGContextFillPath(context);
                    
                    [string_s drawInRect:CGRectMake(x_s, y_s, stringSize_s.width, stringSize_s.height)];
                }
                
                CGContextAddPath(context, path.CGPath);
                CGContextAddPath(context, path1.CGPath);
                CGContextAddPath(context, path2.CGPath);
                CGContextClip(context);
                
                UIImage *img0 = (UIImage *)imageArray[0];
                UIImage *img1 = (UIImage *)imageArray[1];
                UIImage *img2 = (UIImage *)imageArray[2];
                [img0 drawInRect:path.bounds];
                [img1 drawInRect:path1.bounds];
                [img2 drawInRect:path2.bounds];
                
            }else if (imageArray.count == 2){
                for (int i = 0; i < titleArray.count; i ++) {
                    UIColor *color;
                    UIBezierPath *drawPath;
                    
                    NSAttributedString *string_s = (NSAttributedString*)titleArray[i];
                    CGSize stringSize_s = [string_s size];
                    CGFloat x_s = 0;
                    CGFloat y_s = 0;
                    if(i == 1){
                        color = [UIColor purpleColor];
                        drawPath = path2;
                        x_s = drawPath.currentPoint.x + (drawPath.bounds.size.width - stringSize_s.width)/2.0;
                        y_s = drawPath.currentPoint.y + (drawPath.bounds.size.height - stringSize_s.height)/2.0;
                    }else{
                        color = [UIColor redColor];
                        drawPath = path3;
                        
                        x_s = (drawPath.bounds.size.width - stringSize_s.width)/2.0;
                        y_s = drawPath.currentPoint.y + (drawPath.bounds.size.height - stringSize_s.height)/2.0;
                    }
                    
                    CGContextSetFillColorWithColor(context, color.CGColor);
                    CGContextAddPath(context, drawPath.CGPath);
                    CGContextFillPath(context);
                    
                    [string_s drawInRect:CGRectMake(x_s, y_s, stringSize_s.width, stringSize_s.height)];
                }

                
                CGContextAddPath(context, path.CGPath);
                CGContextAddPath(context, path1.CGPath);
                CGContextClip(context);
                
                UIImage *img0 = (UIImage *)imageArray[0];
                UIImage *img1 = (UIImage *)imageArray[1];
                [img0 drawInRect:path.bounds];
                [img1 drawInRect:path1.bounds];
                
            }else if (imageArray.count == 1){
                
                for (int i = 0; i < titleArray.count; i ++) {
                    UIColor *color;
                    UIBezierPath *drawPath;
                    
                    NSAttributedString *string_s = (NSAttributedString*)titleArray[i];
                    CGSize stringSize_s = [string_s size];
                    CGFloat x_s = 0;
                    CGFloat y_s = 0;
                    if(i == 0){
                        color = [UIColor orangeColor];
                        drawPath = path1;
                        x_s = drawPath.currentPoint.x + (drawPath.bounds.size.width - stringSize_s.width)/2.0;
                        y_s = (drawPath.bounds.size.height - stringSize_s.height)/2.0;
                    }else if(i == 1){
                        color = [UIColor purpleColor];
                        drawPath = path2;
                        x_s = drawPath.currentPoint.x + (drawPath.bounds.size.width - stringSize_s.width)/2.0;
                        y_s = drawPath.currentPoint.y + (drawPath.bounds.size.height - stringSize_s.height)/2.0;
                    }else{
                        color = [UIColor redColor];
                        drawPath = path3;
                        
                        x_s = (drawPath.bounds.size.width - stringSize_s.width)/2.0;
                        y_s = drawPath.currentPoint.y + (drawPath.bounds.size.height - stringSize_s.height)/2.0;
                    }
                    
                    CGContextSetFillColorWithColor(context, color.CGColor);
                    CGContextAddPath(context, drawPath.CGPath);
                    CGContextFillPath(context);
                    
                    [string_s drawInRect:CGRectMake(x_s, y_s, stringSize_s.width, stringSize_s.height)];
                }
                
                CGContextAddPath(context, path.CGPath);
                CGContextClip(context);
                
                UIImage *img0 = (UIImage *)imageArray[0];
                [img0 drawInRect:path.bounds];
            }else{
                for (int i = 0; i < titleArray.count; i ++) {
                    UIColor *color;
                    UIBezierPath *drawPath;
                    
                    NSAttributedString *string_s = (NSAttributedString*)titleArray[i];
                    CGSize stringSize_s = [string_s size];
                    CGFloat x_s = 0;
                    CGFloat y_s = 0;
                    if (i == 0) {
                        color = [UIColor greenColor];
                        drawPath = path;
                        x_s = (drawPath.bounds.size.width - stringSize_s.width)/2.0;
                        y_s = (drawPath.bounds.size.height - stringSize_s.height)/2.0;
                    }else if(i == 1){
                        color = [UIColor orangeColor];
                        drawPath = path1;
                        x_s = drawPath.currentPoint.x + (drawPath.bounds.size.width - stringSize_s.width)/2.0;
                        y_s = (drawPath.bounds.size.height - stringSize_s.height)/2.0;
                    }else if(i == 2){
                        color = [UIColor purpleColor];
                        drawPath = path2;
                        x_s = drawPath.currentPoint.x + (drawPath.bounds.size.width - stringSize_s.width)/2.0;
                        y_s = drawPath.currentPoint.y + (drawPath.bounds.size.height - stringSize_s.height)/2.0;
                    }else{
                        color = [UIColor redColor];
                        drawPath = path3;
                        
                        x_s = (drawPath.bounds.size.width - stringSize_s.width)/2.0;
                        y_s = drawPath.currentPoint.y + (drawPath.bounds.size.height - stringSize_s.height)/2.0;
                    }
                    
                    CGContextSetFillColorWithColor(context, color.CGColor);
                    CGContextAddPath(context, drawPath.CGPath);
                    CGContextFillPath(context);

                    [string_s drawInRect:CGRectMake(x_s, y_s, stringSize_s.width, stringSize_s.height)];
                }
            }
            UIImage *newimg = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
            return newimg;
        }
            break;
        default:
            break;
    }
    return nil;
}

由于時(shí)間關(guān)系,代碼比較長(zhǎng),中間文字的繪制的坐標(biāo)稍微有點(diǎn)偏,算的上是我目前為止封裝的比較長(zhǎng)的API了,各位看官得耐心了,多多包涵~
下面還是看看效果圖

four.png

three

最后還是來(lái)個(gè)傳送門

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,108評(píng)論 25 709
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫(kù)、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,407評(píng)論 4 61
  • 愛(ài)一個(gè)女孩,戀愛(ài)35天后,我們分手了。 她一個(gè)人看完《羅曼蒂克消亡史》默默從我的世界路過(guò);我看著《擺渡...
    藤齡閱讀 485評(píng)論 0 2
  • 于我一笑,余音裊裊,一葉扁舟,上下飄搖。 于我一笑,渦里狂濤,一片落花,左右打繞。 一笑于我,始生驕傲:望之往昔,...
    雅俗共賞Y閱讀 181評(píng)論 2 5
  • 南山南,煙雨兩重天,張飛封侯地。 夕陽(yáng)隱江頭,粼光帝宮頂。 雨色錯(cuò)群山,只羨蓑衣翁。 何來(lái)故人嘆,花紅映江面。 南...
    宮塵閱讀 339評(píng)論 0 3

友情鏈接更多精彩內(nèi)容