- 十六进制颜色转换
当颜色值大于6位时,例如:0xFFFFFFFF, 取前两位为alpha值,
- (UIColor *)yxHandleStrToColor {
if (self) {
UIColor *color = [UIColor colorFromHexString_au:self alpha:1];
NSString *tmpColorStr = @"";
if ([self hasPrefix:@"#"]) {
tmpColorStr = [self stringByReplacingOccurrencesOfString:@"#" withString:@""];
}else if ([self hasPrefix:@"0x"]) {
tmpColorStr = [self stringByReplacingOccurrencesOfString:@"0x" withString:@""];
}else {
tmpColorStr = self ?:@"";
}
if (tmpColorStr.length > 6) {
NSString *alphaStr = [self substringToIndex:2];
long colorLong = strtoul([alphaStr cStringUsingEncoding:NSUTF8StringEncoding], 0, 16);
CGFloat yxAlpha = 1;
yxAlpha = (float)colorLong /255.0f;
color = [UIColor colorFromHexString_au:[self substringFromIndex:2] alpha:yxAlpha];
}else {
color = [UIColor colorFromHexString_au:self alpha:1];
}
return color;
}else {
return [UIColor clearColor];
}
}
网友评论