美文网首页
iOS 改变不同范围内的字体样式

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

作者: 花蕊1235 | 来源:发表于2017-12-26 15:47 被阅读0次

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

相关文章

网友评论

      本文标题:iOS 改变不同范围内的字体样式

      本文链接:https://www.haomeiwen.com/subject/rvkjgxtx.html