IOS Coding Project Fragment record (i)

Source: Internet
Author: User

display different font colors depending on the lable status in UIButton
Effect


#import<UIKit/UIKit.h>#defineRGB (r,g,b) [Uicolor colorwithred:r/255.0f green:g/255.0f blue:b/255.0f alpha:1.0f]#defineDefaulttextnomalcolor RGB (167,160,160)#defineDefaulttextselectedcolor [Uicolor colorwithpatternimage:[uiimage imagenamed:[fuserinfo ShareSingleton].navibar_bg_ Image]]@interfaceUIButton (RichText)+ (nsmutableattributedstring*) Titlerichinfostr: (NSString *) strinfo Normalrange: (nsrange) NormalRange NormalColor: ( Uicolor *) NormalColor Selectedrange: (nsrange) Selectedrange selectedcolor: (Uicolor *) Selectedcolor;+ (void) Allbuttonshowstrinfobutton: (UIButton *) button INFOSTR: (NSString *) str;+ (void) Unreadbuttonshowstrinfobutton: (UIButton *) button INFOSTR: (NSString *) str;+ (void) Trashbuttonshowstrinfobutton: (UIButton *) button INFOSTR: (NSString *) str;@end
#import "uibutton+richtext.h"@implementationUIButton (RichText)//sets the color style for different text displays based on length+ (nsmutableattributedstring*) Titlerichinfostr: (NSString *) strinfo Normalrange: (nsrange) NormalRange NormalColor: ( Uicolor *) NormalColor Selectedrange: (nsrange) Selectedrange selectedcolor: (Uicolor *) Selectedcolor {nsmutableattributedstring*attributedstr =[[Nsmutableattributedstring alloc]initwithstring:strinfo];                          [Attributedstr addattribute:nsforegroundcolorattributename Value:normalcolor    Range:normalrange];                          [Attributedstr addattribute:nsforegroundcolorattributename Value:selectedcolor        Range:selectedrange]; returnattributedstr;}+ (void) Allbuttonshowstrinfobutton: (UIButton *) button INFOSTR: (NSString *) str{nsmutableattributedstring*allnormalstr = [Self titlerichinfostr:str normalrange:nsmakerange (0,2) Normalcolor:defaulttextnomalcolor Selectedrange:nsmakerange (2, str.length-2) Selectedcolor:defaulttextnomalcolor]; Nsmutableattributedstring*allselectstr = [Self titlerichinfostr:str normalrange:nsmakerange (0,2) Normalcolor:defaulttextselectedcolor Selectedrange:nsmakerange (2, str.length-2) Selectedcolor:defaulttextnomalcolor];    [Button Setattributedtitle:allnormalstr Forstate:uicontrolstatenormal]; [Button Setattributedtitle:allselectstr forstate:uicontrolstateselected];}+ (void) Unreadbuttonshowstrinfobutton: (UIButton *) button INFOSTR: (NSString *) str {nsmutableattributedstring*attributedstr = [Self titlerichinfostr:str normalrange:nsmakerange (0,2) Normalcolor:defaulttextnomalcolor Selectedrange:nsmakerange (2, str.length-2) Selectedcolor:[uicolor Redcolor]]; Nsmutableattributedstring*attributedseletedstr = [Self titlerichinfostr:str normalrange:nsmakerange (0,2) Normalcolor:defaulttextselectedcolor Selectedrange:nsmakerange (2, str.length-2) Selectedcolor:[uicolor Redcolor]];    [Button Setattributedtitle:attributedstr Forstate:uicontrolstatenormal]; [Button Setattributedtitle:attributedseletedstr forstate:uicontrolstateselected];}+ (void) Trashbuttonshowstrinfobutton: (UIButton *) button INFOSTR: (NSString *) str {nsmutableattributedstring*trashnormalstr = [Self titlerichinfostr:str normalrange:nsmakerange (0,3) Normalcolor:defaulttextnomalcolor Selectedrange:nsmakerange (3, str.length-3) Selectedcolor:defaulttextnomalcolor]; Nsmutableattributedstring*trashselectstr = [Self titlerichinfostr:str normalrange:nsmakerange (0,3) Normalcolor:defaulttextselectedcolor Selectedrange:nsmakerange (3, str.length-3) Selectedcolor:defaulttextnomalcolor];    [Button Setattributedtitle:trashnormalstr Forstate:uicontrolstatenormal]; [Button Setattributedtitle:trashselectstr forstate:uicontrolstateselected];}@end

IOS Coding Project Fragment record (i)

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.