富文本 NSMutableAttributedString

用了个string的Category 扩展了一下

//设置颜色
- (NSMutableAttributedString *)toAttributedStringAndNormalColor:(UIColor *)nolmalColor
                                               AndSeletedString:(NSArray<NSString *> *)selectedStringArray
                                               AndSelectenColor:(UIColor *)selColor{
    NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc]initWithString:self];
    [attributeString setAttributes:@{NSForegroundColorAttributeName :nolmalColor
                                     }
                             range:NSMakeRange(0, self.length)];
    for (NSString *selectedString in selectedStringArray) {
        NSRange selRange = [self rangeOfString:selectedString];
        if (selRange.location != NSNotFound) {
            [attributeString setAttributes:@{NSForegroundColorAttributeName :selColor
                                             }
                                     range: selRange];
        }
    }
    return attributeString;
}

设置行间距

- (void)setAttributedTextTitle:(NSString *)title
                      AndName:(NSString *)name{
    NSString *showString = [NSString stringWithFormat:@"%@\n%@",title,name];
    NSMutableAttributedString *attributedText = [showString toAttributedStringAndNormalColor:[UIColor blackColor] AndSeletedString:@[name] AndSelectenColor:PCH_BANK_GRAY_c8c8c8];
    UIFont *bigFont = [UIFont systemFontOfSize:PCH_BitMap_BY_SIZE(28)];
    UIFont *smallFont = [UIFont systemFontOfSize:PCH_BitMap_BY_SIZE(22)];
    [attributedText addAttribute:NSFontAttributeName value:smallFont range:NSMakeRange(0, showString.length)];
    [attributedText addAttribute:NSFontAttributeName value:bigFont range:NSMakeRange(0, title.length)];
   
    float lineSpacing = PCH_BitMap_BY_SIZE(5);
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
// 换行方式
    [paragraphStyle setLineBreakMode:NSLineBreakByCharWrapping];
 // 间距
    [paragraphStyle setLineSpacing:lineSpacing];
    [attributedText addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, showString.length)];
    ((UILabel *)(self.navigationItem.titleView)).attributedText = attributedText;
}

// 设置大小

- (NSMutableAttributedString *)toAttributedStringAndNormalSize:(float)nolmalSize
                                              AndSeletedString:(NSArray<NSString *> *)selectedStringArray
                                              AndSelectenColor:(float )selSize{
    UIFont *normalFont = [UIFont systemFontOfSize:nolmalSize];
    UIFont *selectFont = [UIFont systemFontOfSize:selSize];
    NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc]initWithString:self];
    [attributeString addAttribute:NSFontAttributeName value:normalFont range:NSMakeRange(0, self.length)];
    for (NSString *selectedString in selectedStringArray) {
        NSRange selRange = [self rangeOfString:selectedString];
        if (selRange.location != NSNotFound) {
            [attributeString addAttribute:NSFontAttributeName value:selectFont range:selRange];
        }
    }
    return attributeString;
}

使用

- (void)setAttributedTextByAreaCode:(NSString *)areaCode
                      AndContryName:(NSString *)contryName{
    _areaCode = areaCode;
    _contryName = contryName;
   
    NSString *showString = [NSString stringWithFormat:@"%@  %@",areaCode,contryName];
    NSMutableAttributedString *attributedText = [showString toAttributedStringAndNormalColor:[UIColor grayColor] AndSeletedString:@[contryName] AndSelectenColor:PCH_Bank_Yellow_ff9805];
    UIFont *bigFont = [UIFont systemFontOfSize:PCH_BitMap_BY_SIZE(30)];
    UIFont *smallFont = [UIFont systemFontOfSize:PCH_BitMap_BY_SIZE(24)];
    [attributedText addAttribute:NSFontAttributeName value:smallFont range:NSMakeRange(0, showString.length)];
    [attributedText addAttribute:NSFontAttributeName value:bigFont range:NSMakeRange(0, contryName.length)];
    self.textFiled.attributedText = attributedText;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容