标题 | 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]; } |
随便看 |
|
在线学习网考试资料包含高考、自考、专升本考试、人事考试、公务员考试、大学生村官考试、特岗教师招聘考试、事业单位招聘考试、企业人才招聘、银行招聘、教师招聘、农村信用社招聘、各类资格证书考试等各类考试资料。