美文网首页
iOS-十六进制颜色转换 0xFFFFFFFF

iOS-十六进制颜色转换 0xFFFFFFFF

作者: 丿小七 | 来源:发表于2022-11-20 21:14 被阅读0次
    • 十六进制颜色转换

    当颜色值大于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];
        }
    }
    

    相关文章

      网友评论

          本文标题:iOS-十六进制颜色转换 0xFFFFFFFF

          本文链接:https://www.haomeiwen.com/subject/asawxdtx.html