iOS 改变不同范围内的字体样式

//  字体的属性列表  颜色  字体大小NSFontAttributeName

        NSDictionary *dictstr = @{

                                  NSForegroundColorAttributeName : [UIColor whiteColor],

                                  NSBackgroundColorAttributeName : [UIColor redColor]

                                  };

//        新建带样式的文字

        NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:_rightLabel.text attributes:dictstr];

//        查找:的范围

        NSRange range1 = [[attrStr string] rangeOfString:@":"];

        NSRange range2 = [[attrStr string] rangeOfString:@"0" options:NSBackwardsSearch];

//        设置:的文字和背景颜色

        [attrStr addAttribute:NSForegroundColorAttributeName value:[UIColor darkGrayColor] range:range1];

        [attrStr addAttribute:NSBackgroundColorAttributeName value:[UIColor clearColor] range:range1];

        [attrStr addAttribute:NSForegroundColorAttributeName value:[UIColor darkGrayColor] range:range2];

        [attrStr addAttribute:NSBackgroundColorAttributeName value:[UIColor clearColor] range:range2];

        [_rightLabel setAttributedText:attrStr];

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容