iOS Development Tips (Series 18: Extended Uicolor, hex color settings supported)

Source: Internet
Author: User
Tags truncated uikit

Create a new category named Uicolor+hex, which indicates that the Uicolor supports hex hex color settings.

Uicolor+hex.h file,

#import<UIKit/UIKit.h>#define Rgba_color (R, G, B, A) [Uicolor colorwithred: ((r)/255.0f) Green: ((G)/255.0f) Blue: ((B)/255.0f) ALPHA:A]#define Rgb_color (R, G, B) [Uicolor colorwithred: ((r)/255.0f) Green: ((G)/255.0f) Blue: ((B)/255.0f) alpha:1.0f]@interface uicolor  ( hex) +  ( uicolor *) colorwithhexstring: ( nsstring *) color; //get color from hex string, //color: support @" #123456 ",  @" 0x123456 ",  @" 123456 "three formats +  (< Span class= "hljs-built_in" >uicolor *) colorwithhexstring: ( nsstring *) Color alpha: ( cgfloat) Alpha;  @end          

The above code begins with two macro definitions, which is a simplification of the [Uicolor ColorWithRed:green:blue:alpha] method, declaring two methods in Uicolor (HEX)- Colorwithhexstring and-colorwithhexstring:alpha, this is very well understood.

UICOLOR+HEX.M file

#import"Uicolor+hex.h"@implementationUicolor(Hex)+ (Uicolor *) Colorwithhexstring: (NSString *) Color alpha: (CGFloat) alpha{Delete spaces in a stringNSString *cstring = [[Color stringbytrimmingcharactersinset:[Nscharacterset Whitespaceandnewlinecharacterset]] uppercasestring];String should be 6 or 8 charactersif ([cString length] <6) {return [Uicolor Clearcolor]; }Strip 0X if it appearsIf it starts with 0x, then the string is truncated, and the string starts at the position indexed to 2, up to the endif ([cString hasprefix:@ "0X"]) {cString = [cString substringfromindex:2]; }If it starts with #, then the string is truncated, and the string starts at the position indexed to 1, up to the endif ([cString hasprefix:@ "#"]) {cString = [cString substringfromindex:1]; }if ([cString length]! =6) {return [Uicolor Clearcolor]; }Separate into R, G, B substringsNsrange range; Range.location =0; Range.length =2;RNSString *rstring = [cString substringwithrange:range];G range.location =2;NSString *gstring = [cString substringwithrange:range];b range.location =4;NSString *bstring = [cString substringwithrange:range];Scan valuesUnsignedint R, G, B; [[Nsscanner Scannerwithstring:rstring] scanhexint:&r]; [[Nsscanner Scannerwithstring:gstring] scanhexint:&g]; [[Nsscanner Scannerwithstring:bstring] scanhexint:&b];return [Uicolor colorwithred: ((float) R/255.0f) Green: ((float) g / 255.0f)  blue: ((float) b /  255.0f)  alpha:alpha];} //the default alpha value is 1+  (uicolor *) colorwithhexstring: ( nsstring *) color{    return [self  Colorwithhexstring:color alpha:1.0f];}  @end          

This expands the uicolor and supports hexadecimal color settings. Here is a chestnut, set UIButton some color features to illustrate the use of the extension,

#import"Uicolor+hex.h"Omit extra codeSet the Barbuttonitem on the right side of the navigation bar to button-(void) setupnavigationitem{UIView *rightview = [[UIView alloc] init]; Rightview.bounds =CGRectMake (0,0,52,44);UIButton *rightbutton = [UIButton Buttonwithtype:Uibuttontypecustom]; Rightbutton.frame =CGRectMake (-6,0,52,44); Rightbutton.backgroundimageedgeinsets =Uiedgeinsetsmake (7,0,7,0);Ksetting is an internationalized string "settings" [Rightbutton settitle:nvslocalizedstring (@ "Ksetting",Nil) Forstate:UIControlStateNormal];Using Rgb_color defined by a macro[Rightbutton Settitlecolor:rgb_color () forstate:uicontrolstatehighlighted];Using Uicolor+hex extension [Rightbutton settitlecolor:[Uicolor colorwithhexstring:@ "#708c3b"] forstate:UIControlStateNormal]; RightButton.titleLabel.font = [Uifont Fontwithname:@ "Heiti SC" size:12.F]; [Rightbutton setbackgroundimage:[UIImage imagenamed:@ "DEVICE_SETTING_BG"] forstate:UIControlStateNormal]; [Rightbutton setbackgroundimage:[UIImage imagenamed:@ "Device_setting_bg_press"] forstate:Uicontrolstatehighlighted]; [Rightbutton AddTarget:Self action: @selector (settingbtnpresss:)            forcontrolevents:uicontroleventtouchupinside];     [rightView addSubview:rightButton];         uibarbuttonitem *rightbarbuttonitem =  [[uibarbuttonitem alloc]  Initwithcustomview:rightview];    [ Self.navigationitem setrightbarbuttonitem:rightbarbuttonitem animated:< Span class= "Hljs-literal" >yes];     [rightbarbuttonitem release];     [rightview release];}           

Well, the use of almost as simple, summed up, this blog mainly has the following several details or knowledge points,

(1) macro definition Rgb_color and Rgba_color can set color

(2) Uicolor+hex extension can set color

(3) How the Barbuttonitem above the navigation bar is set to button

(4) button some common and infrequently used property settings

iOS Development Tips (Series 18: Extended Uicolor, hex color settings supported)

Related Article

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.