欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

富文本 NSMutableAttributedString

程序员文章站 2023-12-24 16:29:45
...

用了个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;
}

上一篇:

下一篇: