iOS字体中划线下划线不同样式拼接富文本

有三种方式

1.不带range,全部改变

NSDictionary *attribtDic = @{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:@"这是一个label的中划线" attributes:attribtDic];
_label.attributedText = attribtStr;

2.带range,只改变range

NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"这是一个btn的下划线"];
NSRange strRange = {0,[str length]-2};
[str addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:strRange];
[_btn setAttributedTitle:str forState:UIControlStateNormal];

3.不同样式拼接

NSString *contentStr = @"不同样式字符串的拼接";
NSString *changeColorStr = @"字符串的拼";
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:contentStr];
NSRange range = [contentStr rangeOfString:changeColorStr];
            
[attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:13] range:range];
[attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:range];
[attributedString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:range];
            
[_btn setAttributedTitle:attributedString forState:UIControlStateNormal];

你可能感兴趣的:(iOS字体中划线下划线不同样式拼接富文本)