iOS Development-16 binary color conversion

Source: Internet
Author: User

The project often uses the color conversion, some through the hexadecimal to the number to the color, wants the simple point to pass through the string directly, simply extends the classification Uicolor, the code is as follows:

+ (Uicolor *) Colorwithhex: (NSString *) hexcolor{return [self Colorwithhex:hexcolor alpha:1.0f];} Http://www.cnblogs.com/xiaofeixiang iOS technology: 228407086+ (Uicolor *) Colorwithhex: (NSString *) Hexcolor Alpha: (float) alpha{//Remove space NSString *colorstr = [[Hexcolor stringbytrimmingcharactersinset:[nscharacterset WhitespaceAndNewlineCh    Aracterset]] uppercasestring]; String should be 6 or 8 characters if ([Colorstr length] < 6| |    [Colorstr length]>7) {return [Uicolor Clearcolor];    }//if ([Colorstr hasprefix:@ "#"]) {colorstr = [colorstr substringfromindex:1];    } Nsrange range;    range.location = 0;    Range.length = 2;    Red NSString *redstring = [Colorstr substringwithrange:range];    Green range.location = 2;    NSString *greenstring = [Colorstr substringwithrange:range];    Blue range.location = 4;        NSString *bluestring= [Colorstr Substringwithrange:range];    Scan values unsigned int red, green, blue; [[NSScanner Scannerwithstring:redstring] scanhexint:&red];    [[Nsscanner scannerwithstring:greenstring] scanhexint:&green];    [[Nsscanner scannerwithstring:bluestring] scanhexint:&blue]; return [Uicolor colorwithred: ((float) red/255.0f) Green: ((float) green/255.0f) Blue: ((float) blue/255.0f) Alpha:alpha] ;}

  

iOS Development-16 binary color conversion

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.