字符串处理

NSMutableAttributedString的使用

在iOS开发中,对NSMutableAttributedString的使用是非常多的
比如:同行lable不同字体,不同颜色,下划线,删除线,以及同行label中间字体颜色不同,这些都是很常见的

同行不同字体

/**
    同行label不同字体
 */
 +(NSMutableAttributedString *)makeStringWith:(NSString *)textStr AndfirstColor:(UIColor *)firstColor AndsecondColor:(UIColor *)secondColor WithfirstFont:(CGFloat )firstFont andsecondFont:(CGFloat)secondFont andLastRange:(NSInteger)lastNumber{
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc]initWithString:textStr];
    NSRange range1;
    NSRange range2;
    range1 = NSMakeRange(0, attr.length - lastNumber);
    range2 = NSMakeRange(attr.length - lastNumber, lastNumber);
    [attr addAttribute:NSForegroundColorAttributeName value:firstColor range:range1];
    [attr addAttribute:NSForegroundColorAttributeName value:secondColor range:range2];
    //使用特定的字体
//    [attr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"AppleSDGothicNeo-Light" size:firstFont] range:range1];
//    [attr addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"AppleSDGothicNeo-Light" size:secondFont] range:range2];
    [attr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:firstFont] range:range1];
    [attr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:secondFont] range:range2];
    return attr;
}

中间单一染色

/**
    中间单一染色
 */
 +(NSMutableAttributedString *)changeStringWith:(NSString *)oldStr andColor:(NSString *)hexStr andLenght:(NSInteger )lengh{
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc]initWithString:oldStr];
    [attr addAttribute:NSForegroundColorAttributeName value:[UIColor colorFromHexString:hexStr] range:NSMakeRange(4, lengh)];
    //颜色方法为16进制颜色复制,在前面的博客中介绍过
    return attr;
}

或者用下面这种方法,同行label不同字体不同颜色,可以具体区分

+(NSMutableAttributedString *)makeStringWith:(NSString *)textStr AndfirstColor:(UIColor *)firstColor AndsecondColor:(UIColor *)secondColor WithfirstFont:(CGFloat )firstFont andsecondFont:(CGFloat)secondFont andThirdColor:(UIColor *)thirdColor andfirstRange:(NSRange )firstRange midRange:(NSRange )midRange lastRange:(NSRange)lastRange{
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc]initWithString:textStr];
    [attr addAttribute:NSForegroundColorAttributeName value:firstColor range:firstRange];
    [attr addAttribute:NSForegroundColorAttributeName value:secondColor range:midRange];
    [attr addAttribute:NSForegroundColorAttributeName value:thirdColor range:lastRange];
    [attr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:firstFont] range:firstRange];
    [attr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:secondFont] range:midRange];
    [attr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:firstFont] range:lastRange];
    return attr;
}

添加下划线

+ (NSMutableAttributedString *)makeALineUnderLabelWithStr:(NSString *)strr andRange:(NSRange)rangee{
    NSMutableAttributedString *mustrr = [[NSMutableAttributedString alloc]initWithString:strr];
    [mustrr addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:rangee];
    [mustrr addAttribute:NSForegroundColorAttributeName value:kRGB(108, 99, 255)   range:rangee];

    //设置下划线颜色...
    [mustrr addAttribute:NSUnderlineColorAttributeName value:kRGB(108, 99, 255)  range:rangee];
    return mustrr;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值