I'd like to pick random number for a different string. The following example is similar to my scenario:
+ (UIColor *)ivl_randomColorWithSeedString:(NSString *)seedString {
srand48(seedString ? seedString.hash : arc4random());
float red = 0.0;
while (red < 0.1 || red > 0.84) {
red = drand48();
}
float green = 0.0;
while (green < 0.1 || green > 0.84) {
green = drand48();
}
float blue = 0.0;
while (blue < 0.1 || blue > 0.84) {
blue = drand48();
}
return [UIColor colorWithRed:red green:green blue:blue alpha:1.0f];
}
what I'd like to achieve is getting a random color for a different string, if I have the same string then return the same index.
- (void)setImageWithImage:(UIImage *)image orString:(NSString *)string colorArray:(NSArray *)colorArray circular:(BOOL)isCircular textAttributes:(NSDictionary *)textAttributes {
if (!textAttributes) {
textAttributes = @{
NSFontAttributeName: [self fontForFontName:nil],
NSForegroundColorAttributeName: [UIColor whiteColor]
};
}
NSMutableString *displayString = [NSMutableString stringWithString:@""];
NSMutableArray *words = [[string componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] mutableCopy];
//
// Get first letter of the first and last word
//
if ([words count]) {
NSString *firstWord = [words firstObject];
if ([firstWord length]) {
// Get character range to handle emoji (emojis consist of 2 characters in sequence)
NSRange firstLetterRange = [firstWord rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, 1)];
[displayString appendString:[firstWord substringWithRange:firstLetterRange]];
}
if ([words count] >= 2) {
NSString *lastWord = [words lastObject];
while ([lastWord length] == 0 && [words count] >= 2) {
[words removeLastObject];
lastWord = [words lastObject];
}
if ([words count] > 1) {
// Get character range to handle emoji (emojis consist of 2 characters in sequence)
NSRange lastLetterRange = [lastWord rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, 1)];
[displayString appendString:[lastWord substringWithRange:lastLetterRange]];
}
}
}
srand48(string ? string.hash : arc4random());
NSUInteger randomIndex = arc4random() % [colorArray count];
UIColor *backgroundColor = colorArray[randomIndex];
if (image != nil) {
self.image = image;
} else {
self.image = [self imageSnapshotFromText:[displayString uppercaseString] backgroundColor:backgroundColor circular:isCircular textAttributes:textAttributes];
}
}
What's the best approach on this?
Aucun commentaire:
Enregistrer un commentaire