【问题标题】:how to convert hexcode color into uicolor (svg image)如何将十六进制颜色转换为uicolor(svg图像)
【发布时间】:2014-09-19 10:31:09
【问题描述】:

我在我的程序中使用 svg 图像。首先我需要使用它的坐标绘制图像的轮廓,然后应该出现原始图像中的颜色的真实颜色。我使用了一些代码将十六进制代码更改为UIColor,但它不适用于每个图像。

请建议我一些可以用于 evry 图像的代码!

这是我使用的代码

-(UIColor *)colorWithHexString {

/* convert the string into a int */
unsigned int colorValueR,colorValueG,colorValueB,colorValueA;
UIColor *color;
NSString *hexStringCleared = [[[PocketSVG sharedManager]colorstr] stringByReplacingOccurrencesOfString:@"#" withString:@""];
if(hexStringCleared.length == 3) {
    /* short color form */

    hexStringCleared = [NSString stringWithFormat:@"%@%@%@%@%@%@", [hexStringCleared substringWithRange:NSMakeRange(0, 1)],[hexStringCleared substringWithRange:NSMakeRange(0, 1)],
                        [hexStringCleared substringWithRange:NSMakeRange(1, 1)],[hexStringCleared substringWithRange:NSMakeRange(1, 1)],
                        [hexStringCleared substringWithRange:NSMakeRange(2, 1)],[hexStringCleared substringWithRange:NSMakeRange(2, 1)]];
}
if(hexStringCleared.length == 6) {
    hexStringCleared = [hexStringCleared stringByAppendingString:@"ff"];
}

if ([hexStringCleared isEqualToString:@"none"]) {
    color=[UIColor clearColor];
}
if ([hexStringCleared isEqualToString:@"url(#SVGID_1_)"] || [hexStringCleared isEqualToString:@"url(#SVGID_2_)"] ||[hexStringCleared isEqualToString:@"url(#SVGID_3_)"] || [hexStringCleared isEqualToString:@"url(#SVGID_2_)"]) {
    color=[UIColor clearColor];
}


NSString *red = [hexStringCleared substringWithRange:NSMakeRange(0, 2)];
NSString *green = [hexStringCleared substringWithRange:NSMakeRange(2, 2)];
NSString *blue = [hexStringCleared substringWithRange:NSMakeRange(4, 2)];
NSString *alpha = [hexStringCleared substringWithRange:NSMakeRange(6, 2)];

[[NSScanner scannerWithString:red] scanHexInt:&colorValueR];
[[NSScanner scannerWithString:green] scanHexInt:&colorValueG];
[[NSScanner scannerWithString:blue] scanHexInt:&colorValueB];
[[NSScanner scannerWithString:alpha] scanHexInt:&colorValueA];

color=[UIColor colorWithRed:((colorValueR)&0xFF)/255.0
                      green:((colorValueG)&0xFF)/255.0
                       blue:((colorValueB)&0xFF)/255.0
                      alpha:((colorValueA)&0xFF)/255.0];
return color;

}

【问题讨论】:

    标签: ios xcode image svg uicolor


    【解决方案1】:

    将此添加到您的 .pch 文件中

    #define UIColorFromRGB(rgbValue) [UIColor \
    colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 \
    green:((float)((rgbValue & 0xFF00) >> 8))/255.0 \
    blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
    

    像这样访问它

    [[[self navigationController] navigationBar] setBarTintColor:UIColorFromRGB(0x3c57ae)];
    

    【讨论】:

    • 不工作,你能解释一下代码吗,实际上我已经将十六进制代码存储在 nsstring 类型对象中。
    • 考虑颜色代码 3c57ae。 3c 代表红色,57 代表绿色,ae 代表蓝色
    • 实际上颜色代码是从另一个函数传递的:(
    • 你从哪里得到颜色?正如你所说,它是字符串形式的。你能发布一些样本吗?
    【解决方案2】:

    如果你查看这个文件:SVGUtilities.m你会发现一个用objective-c写的方法:

    UIColor* UIColorFromSVGColorString (NSString * stringToConvert)
    

    这会将十六进制和许多标准命名颜色转换为 UIColor。我写的。

    【讨论】:

      【解决方案3】:

      您可以使用此库将 Hex 转换为 UIColor。

      https://github.com/burhanuddin353/TFTColor

      【讨论】:

        猜你喜欢
        • 2014-01-28
        • 2020-07-15
        • 2016-09-20
        • 2019-10-29
        • 2016-09-03
        • 2012-09-06
        • 2022-08-10
        • 1970-01-01
        • 2016-12-27
        相关资源
        最近更新 更多