标题 | ios中十六进制的颜色转换为uicolor |
范文 | 可用类别写一个 方便使用 + (uicolor *) colorwithhexstring: (nsstring *)color { nsstring *cstring = [[colorstringbytrimmingcharactersinset:[nscharactersetwhitespaceandnewlinecharacterset]] uppercasestring]; // string should be 6 or 8 characters if ([cstring length] <6) { return [uicolorclearcolor]; } // strip 0x if it appears if ([cstring hasprefix:@0x]) cstring = [cstringsubstringfromindex:2]; if ([cstring hasprefix:@#]) cstring = [cstringsubstringfromindex:1]; if ([cstring length] !=6) return [uicolorclearcolor]; // separate into r, g, b substrings nsrange range; range.location =0; range.length =2; //r nsstring *rstring = [cstring substringwithrange:range]; //g range.location =2; nsstring *gstring = [cstring substringwithrange:range]; //b range.location =4; nsstring *bstring = [cstring substringwithrange:range]; // scan values unsigned int r, g, b; [[nsscannerscannerwithstring:rstring] scanhexint:&r]; [[nsscannerscannerwithstring:gstring] scanhexint:&g]; [[nsscannerscannerwithstring:bstring] scanhexint:&b]; return [uicolorcolorwithred:((float) r /255.0f) green:((float) g /255.0f) blue:((float) b /255.0f) alpha:1.0f]; } |
随便看 |
|
在线学习网范文大全提供好词好句、学习总结、工作总结、演讲稿等写作素材及范文模板,是学习及工作的有利工具。