String Scan class Nsscanner in iOS

Source: Internet
Author: User

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>#defineRgba_color (R, G, B, A) [Uicolor colorwithred: ((r)/255.0f) Green: ((G)/255.0f) Blue: ((B)/255.0f) ALPHA:A]#defineRgb_color (R, G, B) [Uicolor colorwithred: ((r)/255.0f) Green: ((G)/255.0f) Blue: ((B)/255.0f) alpha:1.0f]@interfaceUicolor (Hex)+ (Uicolor *) colorwithhexstring: (NSString *) color;//gets the color from the hexadecimal string,//Color: support @ "#123456", @ "0x123456", @ "123456" three formats+ (Uicolor *) colorwithhexstring: (NSString *) Color alpha: (cgfloat) Alpha;@endThe above code begins with two macro definitions, which is the simplification of the [Uicolor ColorWithRed:green:blue:alpha] method, declaring two methods in Uicolor (Hex)-colorwithhexstring and-Colorwithhexstring:alpha, this is a good understanding. 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 characters    if([cString Length] <6)    {        return[Uicolor Clearcolor]; }    //strip 0X If it appears//if it starts with 0x, then the string is truncated, and the string starts at the position indexed to 2, up to the end    if([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 end    if([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]; //gRange.location =2; NSString*gstring =[cString Substringwithrange:range]; //bRange.location =4; NSString*bstring =[cString Substringwithrange:range]; //Scan ValuesUnsignedintR, 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];}@endthis 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 Code//Set the Barbuttonitem button to the right of the navigation bar- (void) setupnavigationitem{UIView*rightview =[[UIView alloc] init]; Rightview.bounds= CGRectMake (0,0, the, -); UIButton*rightbutton =[UIButton Buttonwithtype:uibuttontypecustom]; Rightbutton.frame= CGRectMake (-6,0, the, -); 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 extensions[Rightbutton settitlecolor:[uicolor colorwithhexstring:@"#708c3b"] Forstate:uicontrolstatenormal]; RightButton.titleLabel.font= [Uifont fontwithname:@"Heiti SC"Size A. 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: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, (1macros define Rgb_color and Rgba_color to set the color (2) uicolor+Hex Extensions can set the color (3How does the barbuttonitem above the navigation bar be set to button (4button some of the common and infrequently used property settings

String Scan class Nsscanner in iOS

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.