iOS development-Get uicolor from 16 binary colors

Source: Internet
Author: User

Currently set Uicolor in iOS can only use its enumeration values, RGB and other methods, can not directly convert the commonly used 16 color value directly into the Uicolor object, so write a point code, the 16 color value into Uicolor.

Code as follows,

Header file
#import <Foundation/Foundation.h>#import <UIKit/UIKit.h>@interface Textservcie:nsobject+ (Uicolor *) Getcolorfromhex: (NSString *) hex; @end

. m

#import "TextServcie.h"@implementationTextservcie+ (Uicolor *) Getcolorfromhex: (NSString *) hex{Hex= [[Hex uppercasestring] Substringfromindex:1]; CGFloat valuearray[3]; Nsarray*strarray=[nsarray Arraywithobjects:[hex Substringwithrange:nsmakerange (0,2)],[hex Substringwithrange:nsmakerange (2,2)],[hex Substringwithrange:nsmakerange (4,2)], nil];  for(intI=0; i<strarray.count;i++) {hex=Strarray[i]; CGFloat value= ([Hex characteratindex:0]-'A'+Ten)*16.0f+[hex Characteratindex:1]-'A'+Ten; Valuearray[i]=value; }    return[Uicolor colorwithred:valuearray[0]/255green:valuearray[1]/255blue:valuearray[2]/255Alpha1];}@end

The above code, working properly, next time no more tools to manually turn RGB

iOS development-Get uicolor from 16 binary colors

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.