最近因?yàn)轫?xiàng)目需求组底,需要根據(jù)不同的nikeName給不同的群設(shè)置含有群名的群頭像。
效果如下:
大概思路就是喻犁,定義幾種顏色作為隨機(jī)使用的底色蔓搞,根據(jù)string對應(yīng)的hash值除以顏色的數(shù)量取余的絕對值來確定每個(gè)string對應(yīng)的顏色,然后繪制一個(gè)對應(yīng)顏色的純色圖片凄鼻,再在圖片中心繪制需要顯示的文字腊瑟。這樣就可以得到想要的效果了聚假。
顯示文本的篩選方法是:含有字母就取前兩個(gè)字符,不含字母的話闰非,長度為1就取一個(gè)字符膘格,長度為2就取兩個(gè)字符,長度為3和4取后兩個(gè)字符财松,長度超過4默認(rèn)取前兩個(gè)字符顯示瘪贱。
寫了個(gè)分類,方便調(diào)用
UIImage+JWNameImage.h
//
// UIImage+JWNameImage.h
// HMClient
//
// Created by jasonwang on 2017/5/18.
// Copyright ? 2017年 YinQ. All rights reserved.
// 根據(jù)string返回對應(yīng)頭像圖片分類
#import <UIKit/UIKit.h>
@interface UIImage (JWNameImage)
+ (UIImage *)JW_acquireNameImageWithNameString:(NSString *)string imageSize:(CGSize)size;
@end
UIImage+JWNameImage.m
//
// UIImage+JWNameImage.m
// HMClient
//
// Created by jasonwang on 2017/5/18.
// Copyright ? 2017年 YinQ. All rights reserved.
//
#import "UIImage+JWNameImage.h"
@implementation UIImage (JWNameImage)
+ (UIImage *)JW_acquireNameImageWithNameString:(NSString *)string imageSize:(CGSize)size {
return [UIImage JW_createNikeNameImageName:[UIImage JW_dealWithNikeName:string] imageSize:size];
}
// 按規(guī)則截取nikeName
+ (NSString *)JW_dealWithNikeName:(NSString *)nikeName {
// 篩除部分特殊符號
NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@"【】"];
nikeName = [nikeName stringByTrimmingCharactersInSet:set];
NSString *showName = @"";
NSString *tempName = @"";
NSRange range1 = [nikeName rangeOfString:@"-"];
if (range1.length) {
// 含有“-”
tempName = [nikeName substringToIndex:range1.location];
}
else {
// 不含“-”
tempName = nikeName;
}
NSRange range2 = [tempName rangeOfString:@"("];
if (range2.length) {
// 含有“(”
tempName = [tempName substringToIndex:range2.location];
}
else {
// 不含“(”
tempName = tempName;
}
if ([UIImage JW_isStringContainLetterWith:tempName]) {
// 含有字母取前兩個(gè)
showName = [tempName substringToIndex:2];
}
else {
// 不含字母
if (!tempName.length) {
}
else if (tempName.length == 1)
{
showName = [tempName substringToIndex:1];
}
else if (tempName.length == 2)
{
showName = [tempName substringToIndex:2];
}
else if (tempName.length == 3)
{
showName = [tempName substringFromIndex:1];
}
else if (tempName.length == 4)
{
showName = [tempName substringFromIndex:2];
}
else {
showName = [tempName substringToIndex:2];
}
}
return showName;
}
// 檢查是否含有字母
+ (BOOL)JW_isStringContainLetterWith:(NSString *)str {
if (!str) {
return NO;
}
NSRegularExpression *numberRegular = [NSRegularExpression regularExpressionWithPattern:@"[A-Za-z]" options:NSRegularExpressionCaseInsensitive error:nil];
NSInteger count = [numberRegular numberOfMatchesInString:str options:NSMatchingReportProgress range:NSMakeRange(0, str.length)];
//count是str中包含[A-Za-z]數(shù)字的個(gè)數(shù)辆毡,只要count>0菜秦,說明str中包含數(shù)字
if (count > 0) {
return YES;
}
return NO;
}
// 根據(jù)nikeName繪制圖片
+ (UIImage *)JW_createNikeNameImageName:(NSString *)name imageSize:(CGSize)size{
NSArray *colorArr = @[@"17c295",@"b38979",@"f2725e",@"f7b55e",@"4da9eb",@"5f70a7",@"568aad"];
UIImage *image = [UIImage JW_imageColor:[UIImage JW_colorWithHexString:colorArr[ABS(name.hash % colorArr.count)] alpha:1.0] size:size cornerRadius:size.width / 2];
UIGraphicsBeginImageContextWithOptions (size, NO , 0.0 );
[image drawAtPoint : CGPointMake ( 0 , 0 )];
// 獲得一個(gè)位圖圖形上下文
CGContextRef context= UIGraphicsGetCurrentContext ();
CGContextDrawPath (context, kCGPathStroke );
// 畫名字
CGSize nameSize = [name sizeWithAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:14]}];
[name drawAtPoint : CGPointMake ( (size.width - nameSize.width) / 2 , (size.height - nameSize.height) / 2 ) withAttributes : @{ NSFontAttributeName :[ UIFont systemFontOfSize:14], NSForegroundColorAttributeName :[ UIColor colorWithHexString:@"ffffff" ] } ];
// 返回繪制的新圖形
UIImage *newImage= UIGraphicsGetImageFromCurrentImageContext ();
UIGraphicsEndImageContext ();
return newImage;
}
+ (UIImage *)JW_imageColor:(UIColor *)color size:(CGSize)size cornerRadius:(CGFloat)radius {
if (CGSizeEqualToSize(size, CGSizeZero)) {
size = CGSizeMake(1, 1);
}
CGRect rect = CGRectMake(0, 0, size.width, size.height);
UIGraphicsBeginImageContextWithOptions(rect.size, 0, [UIScreen mainScreen].scale);
[color set];
UIRectFill(rect);
UIImage *colorImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIGraphicsBeginImageContextWithOptions(size, 0, [UIScreen mainScreen].scale);
[[UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:radius] addClip];
[colorImage drawInRect:rect];
colorImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return colorImage;
}
+ (id)JW_colorWithHexString:(NSString*)hexColor alpha:(CGFloat)alpha {
unsigned int red,green,blue;
NSRange range;
range.length = 2;
range.location = 0;
[[NSScanner scannerWithString:[hexColor substringWithRange:range]]scanHexInt:&red];
range.location = 2;
[[NSScanner scannerWithString:[hexColor substringWithRange:range]]scanHexInt:&green];
range.location = 4;
[[NSScanner scannerWithString:[hexColor substringWithRange:range]]scanHexInt:&blue];
UIColor* retColor = [UIColor colorWithRed:(float)(red/255.0f)green:(float)(green / 255.0f) blue:(float)(blue / 255.0f)alpha:alpha];
return retColor;
}
@end