• iOS Coding项目片段记录(一)


    根据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
  • 相关阅读:
    ES6判断对象是否为空
    mui、拍照、个推推送消息【问题链接】
    查找SAP某个Tcode下已经实施的增强
    MySQL 事务
    Go 学习线路图
    Nginx 限流配置
    Redis 内存优化
    2021年 github被墙最新hosts-每日更新
    Nginx 反向代理与负载均衡详解
    完美实现跨域 iframe 高度自适应
  • 原文地址:https://www.cnblogs.com/741162830qq/p/6030614.html
Copyright © 2020-2023  润新知