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

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

作者: XPGG | 来源:发表于2020-09-21 16:30 被阅读0次

    有三种方式

    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字体中划线下划线不同样式拼接富文本

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