[ios]代码库
+ (UIColor *)colorWithHexString:(NSString *)stringToConvert
{
// Filter the special characters
NSString *cString = [[stringToConvert stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] uppercaseString];
if ([cString length] < 6)
return [UIColor whiteColor];
if ([cString hasPrefix:@"#"])
cString = [cString substringFromIndex:1];
if ([cString length] != 6)
return [UIColor whiteColor];
// subString
NSString *rString = [cString substringWithRange:(NSRange){0,2}];
NSString *gString = [cString substringWithRange:(NSRange){2,2}];
NSString *bString = [cString substringWithRange:(NSRange){4,2}];
// get rgb Values
unsigned int r, g, b;
[[NSScanner scannerWithString:rString] scanHexInt:&r];
[[NSScanner scannerWithString:gString] scanHexInt:&g];
[[NSScanner scannerWithString:bString] scanHexInt:&b];
// retrun color
return [UIColor colorWithRed:((float) r / 255.0f)
green:((float) g / 255.0f)
blue:((float) b / 255.0f)
alpha:1.0f];
}
/*
NSCharacterSet *set = [NSCharacterSet characterSetWithCharactersInString:@"@/:;()¥「」"、[]{}#%-*+=_\\|~<>$€^•'@#$%^&*()_+'\""];
由于NSString中有全角符号和半角符号, 因此有些符号要包括全角和半角的
然后调用stringByTrimmingCharactersInSet
NSString *trimmedString = [string stringByTrimmingCharactersInSet:set];
trimmedString就是过滤后的字符串
*/
by: 发表于:2017-10-13 10:11:02 顶(0) | 踩(0) 回复
??
回复评论