在Objective c ios 7中计算任意字符串的十六进制颜色代码

kev*_*vin 0 random hex colors objective-c

我需要根据用户名/名称/任何字符串以伪随机一致的方式分配用户配置文件颜色.

我如何在Objective C iOS 7中执行此操作?

基于Java的示例就在这里

计算任意字符串的十六进制颜色代码

rma*_*ddy 5

可能有很多方法.这是一个:

NSString *someString = ... // some string to "convert" to a color
NSInteger hash = someString.hash;
int red = (hash >> 16) & 0xFF;
int green = (hash >> 8) & 0xFF;
int blue = hash & 0xFF;
UIColor *someColor = [UIColor colorWithRed:red / 255.0 green:green / 255.0 blue:blue / 255.0 alpha:1.0];
Run Code Online (Sandbox Code Playgroud)

相同的字符串将始终提供相同的颜色.不同的字符串通常会给出不同的颜色,但两个不同的字符串可能会产生相同的颜色.