Ios 来自字符串的特定颜色

Ios 来自字符串的特定颜色,ios,objective-c,algorithm,colors,Ios,Objective C,Algorithm,Colors,我有一个uitableview,它由一个从网站提取数据的字符串填充,因此每个单元格都有一个新字符串。为此,我希望根据单元格中的文本为用户显示一个十六进制 我一直在努力做到这一点,但运气不好,但幸运的是找到了一个javascript脚本,它完成了我试图做的事情。这个脚本,我现在需要转换成obj-c,我自己也尝试过,但失败了。我希望得到一些帮助 javascript: 我在obj-c中的尝试(这里的字符串不是基于来自web的数据,只是一个数组): 也许这不是唯一的错误。改变 hash = [[str

我有一个uitableview,它由一个从网站提取数据的字符串填充,因此每个单元格都有一个新字符串。为此,我希望根据单元格中的文本为用户显示一个十六进制

我一直在努力做到这一点,但运气不好,但幸运的是找到了一个javascript脚本,它完成了我试图做的事情。这个脚本,我现在需要转换成obj-c,我自己也尝试过,但失败了。我希望得到一些帮助

javascript:

我在obj-c中的尝试(这里的字符串不是基于来自web的数据,只是一个数组):


也许这不是唯一的错误。改变

hash = [[strings objectAtIndex:indexPath.row] characterAtIndex:i] + ((hash < 5) - hash);

更新2:

我又修复了一个错误并简化了代码:

unsigned int hash = 0;

NSArray *strings = [NSArray arrayWithObjects:@"MA", @"Ty", @"Ad", @"ER", nil];
NSString *string = [strings objectAtIndex:indexPath.row];

for (int i = 0; i < string.length; i++) {
    hash = [string characterAtIndex:i] + ((hash << 5) - hash);
}

NSString *color = [NSString stringWithFormat:@"#%06x", hash % 0x1000000];
NSLog(@"%@", color);
unsigned int hash=0;
NSArray*字符串=[NSArray ARRAY WITH OBJECTS:@“MA”@“Ty”@“Ad”@“ER”,nil];
NSString*string=[strings objectAtIndex:indexath.row];
for(int i=0;i哈希=[字符串字符索引:i]+((hash-Well它改变了一些,但是一些hex仍然是短的或长的#14890#165100#6780#17380@magnuskahr我已经更新了答案,请检查。它工作得很好!但是它只能在绿色和蓝色的范围内生成颜色,你知道怎样才能在所有颜色的范围内生成不同的颜色吗?@magnuskahr你注意到了吗d我在上一个版本中将
unichar hash
更改为
unsigned int hash
?它应该可以解决这个问题。不,对不起,我错过了:-)你是对的,它工作得很好,非常感谢你的帮助!
hash = [[strings objectAtIndex:indexPath.row] characterAtIndex:i] + ((hash < 5) - hash);
hash = [[strings objectAtIndex:indexPath.row] characterAtIndex:i] + ((hash << 5) - hash);
colour = [NSString stringWithFormat:@"%@%d", colour, value];
colour = [NSString stringWithFormat:@"%@%02x", colour, (unsigned int)value];
unsigned int hash = 0;

NSArray *strings = [NSArray arrayWithObjects:@"MA", @"Ty", @"Ad", @"ER", nil];
NSString *string = [strings objectAtIndex:indexPath.row];

for (int i = 0; i < string.length; i++) {
    hash = [string characterAtIndex:i] + ((hash << 5) - hash);
}

NSString *color = [NSString stringWithFormat:@"#%06x", hash % 0x1000000];
NSLog(@"%@", color);