根据UIbutton中lable状态的不同显示不同的字体颜色效果如图: #import <UIKit/UIKit.h> #define RGB(r,g,b) [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:1.0f] #define DefaultTextNomalColor RGB(167,160,160) #define DefaultTextSelectedColor [UIColor colorWithPatternImage:[UIImage imageNamed:[FUserInfo shareSingleton].navibar_bg_image]] @interface UIButton (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" @implementation UIButton (richText) //根据长度来设置不同文字显示的颜色 样式 + (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]; return AttributedStr; } + (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
时间: 2024-11-14 15:03:32