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

2017-12-26  本文已影响0人  花蕊1235

//  字体的属性列表  颜色  字体大小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];

上一篇 下一篇

猜你喜欢

热点阅读