接到一个需求,根据用户账号显示默认头像。背景色根据账号执行一个算法获取到,头像上显示用户名首字。
安卓算法 colors.get(Math.abs(account.hashCode()) % colors.size());
字符串account取hashCode数值的绝对值和颜色列表总数取余即所需颜色数值。
颜色列表
Arrays.asList(
0xff704d4e,
0xff973444,
0xff546b83,
0xff2578b5,
0xff5c8987,
0xffb49436,
0xfffcb1aa,
0xffe57373,
0xfff06292,
0xff9575cd,
0xff7986cb,
0xff64b5f6,
0xff4fc3f7,
0xff4dd0e1,
0xff4db6ac,
0xff81c784,
0xffaed581,
0xffff8a65,
0xffffd54f,
0xffffb74d,
0xffa1887f,
0xff90a4ae
);
现在转换为iOS代码
以下面的账号和用户名为例:
NSString*account =@"[email protected]";
NSString*name =@"伊布";
首先处理颜色列表和颜色算法;
1、颜色列表里的颜色数值中0x后面的ff表示透明度,这个我不需要,所以去掉这个数值,我只要后6位数值即可:
- (NSArray*)colorLists
{
return@[@"0x704d4e",
@"0x973444",
@"0x546b83",
@"0x2578b5",
@"0x5c8987",
@"0xb49436",
@"0xfcb1aa",
@"0xe57373",
@"0xf06292",
@"0x9575cd",
@"0x7986cb",
@"0x64b5f6",
@"0x4fc3f7",
@"0x4dd0e1",
@"0x4db6ac",
@"0x81c784",
@"0xaed581",
@"0xff8a65",
@"0xffd54f",
@"0xffb74d",
@"0xa1887f",
@"0x90a4ae"];
}
2、hashCode
OC中没有找到对应算法,网上找到一个根据java算法写的OC代码,原文是写了一个NSString的类别,这样可以在项目中任何地方调用,我做了处理仅在当前类调用:
-(int)DF_hashCode:(NSString*)str {
inthash =0;
for(inti =0; i<[str length]; i++) {
NSString*s = [str substringWithRange:NSMakeRange(i,1)];
char*unicode = (char*)[s cStringUsingEncoding:NSUnicodeStringEncoding];
intcharactorUnicode =0;
size_tlength =strlen(unicode);
for(intn =0; n < length; n ++) {
charactorUnicode += (int)((unicode[n] &0xff) << (n *sizeof(char) *8));
}
hash = hash *31+ charactorUnicode;
}
returnhash;
}
3、根据算法计算取颜色
NSArray*colors = [self colorLists]; // 获取颜色列表
int hashCode = [self DF_hashCode:text]; // hashCode
int hashAbs =abs(hashCode); // 取绝对值
int num = hashAbs % colors.count; // 绝对值和颜色总数取余即颜色对应的字符串在数组中的位置
NSString*theColor = colors[num];
4、十六进制颜色数值获取
这里是从网上找到的一个算法,因为这个算法处理十六进制字符串去掉0x只用到6位,所以上面处理颜色时把表示透明度的ff参数去掉:
头文件
#import
#define RGBA_COLOR(R, G, B, A) [UIColor colorWithRed:((R) /255.0f) green:((G) /255.0f) blue:((B) /255.0f) alpha:A]
#define RGB_COLOR(R, G, B) [UIColor colorWithRed:((R) /255.0f) green:((G) /255.0f) blue:((B) /255.0f) alpha:1.0f]
@interfaceUIColor (Hex)
+ (UIColor*)colorWithHexString:(NSString*)color;
//从十六进制字符串获取颜色,
//color:支持@“#123456”、@“0X123456”、@“123456”三种格式
+ (UIColor*)colorWithHexString:(NSString*)color alpha:(CGFloat)alpha;
@end
.m文件
#import"UIColor+Hex.h"
@implementationUIColor (Hex)
+ (UIColor*)colorWithHexString:(NSString*)color alpha:(CGFloat)alpha
{
//删除字符串中的空格
NSString*cString = [[color stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]uppercaseString];
// String should be 6 or 8 characters
if([cString length] <6)
{
return[UIColor clearColor];
}
// strip 0X if it appears
//如果是0x开头的,那么截取字符串,字符串从索引为2的位置开始,一直到末尾
if([cString hasPrefix:@"0X"])
{
cString = [cString substringFromIndex:2];
}
//如果是#开头的,那么截取字符串,字符串从索引为1的位置开始,一直到末尾
if([cString hasPrefix:@"#"])
{
cString = [cString substringFromIndex:1];
}
if([cString length] !=6)
{
return[UIColor clearColor];
}
// Separate into r, g, b substrings
NSRangerange;
range.location=0;
range.length=2;
//r
NSString*rString = [cString substringWithRange:range];
//g
range.location=2;
NSString*gString = [cString substringWithRange:range];
//b
range.location=4;
NSString*bString = [cString substringWithRange:range];
// Scan values
unsignedintr, g, b;
[[NSScanner scannerWithString:rString]scanHexInt:&r];
[[NSScanner scannerWithString:gString]scanHexInt:&g];
[[NSScanner scannerWithString:bString]scanHexInt:&b];
return[UIColor colorWithRed:((float)r /255.0f)green:((float)g /255.0f)blue:((float)b /255.0f)alpha:alpha];
}
//默认alpha值为1
+ (UIColor*)colorWithHexString:(NSString*)color
{
return[self colorWithHexString:coloralpha:1.0f];
}
@end
/////////////
5、获取颜色数值
UIColor*color = [UIColor colorWithHexString:theColor];
6、获取用户名首字并转换为大写(小写字母显示不居中)
NSString*str = [namesubstringToIndex:1]; // 获取首字
str = [strcapitalizedStringWithLocale:[NSLocalecurrentLocale]]; // 转换大写
7、根据颜色和文字生成图片
- (UIImage*)createImage:(CGRect)rect withColor:(UIColor*)color str:(NSString*)str
{
UIGraphicsBeginImageContext(rect.size);
CGContextRefcontext =UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
NSMutableDictionary*attrs = [NSMutableDictionary dictionary];
NSIntegerfontSize = rect.size.width*2/3.0; //
attrs[NSFontAttributeName] = [UIFontsystemFontOfSize:fontSize];
attrs[NSForegroundColorAttributeName] = [UIColorwhiteColor];
CGSizestrSize = [strsizeWithAttributes:attrs];
[strdrawAtPoint:CGPointMake(rect.size.width/2- strSize.width/2, (rect.size.height- strSize.height)/2)withAttributes:attrs];
UIImage*image =UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
returnimage;
}
8、
UIImage*image = [selfcreateImage:self.imageView.boundswithColor:colorstr:str];