利用NSMutableAttributedString属性进行简单的富文本,修改字体大小、字体颜色、文字开头添加图片、价格的中划线

自己创建一个NSString的类别:
创建一个回调block:typedef void(^AttributedBlock)(id data);
1.根据需求页面需要修改一个label里面的文字颜色及大小:

/**
 currentString   当前不需要改变的文字
 chageString     当前需要改变的文字
 endString       结尾字符串
 fontSize        改变的字体大小
 stringColor     改变的颜色
 tag 1:改变颜色。2:改变大小 3.改变颜色和字体
 */

/**改变字体颜色大小*/
+(void)stringWithCurrentString:(NSString *)currentString
              withChangeString:(NSString *)chageString
                     withColor:(UIColor *)stringColor
                       withTag:(NSInteger)tag
                      withFont:(NSInteger)fontSize
                 withEndString:(NSString *)endString
                     withBlock:(AttributedBlock)block;
+(void)stringWithCurrentString:(NSString *)currentString
              withChangeString:(NSString *)chageString
                     withColor:(UIColor *)stringColor
                       withTag:(NSInteger)tag
                      withFont:(NSInteger)fontSize
                 withEndString:(NSString *)endString
                     withBlock:(AttributedBlock)block
{
    NSString * textString = [NSString stringWithFormat:@"%@%@%@",currentString,chageString,endString];
    NSInteger currentStringLength = currentString.length;
    NSInteger chageStringLength = chageString.length;
    NSMutableAttributedString * attributeString = [[NSMutableAttributedString alloc]initWithString:textString];
    if (tag == 1)//改变颜色
    {
        [attributeString addAttribute:NSForegroundColorAttributeName value:stringColor range:NSMakeRange(currentStringLength, chageStringLength)];
    }
    else if (tag == 2)
    {
        [attributeString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:fontSize]    range:NSMakeRange(currentStringLength, chageStringLength)];
    }
    else if (tag == 3)
    {
        [attributeString addAttribute:NSForegroundColorAttributeName value:stringColor range:NSMakeRange(currentStringLength, chageStringLength)];
        [attributeString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:fontSize]    range:NSMakeRange(currentStringLength, chageStringLength)];
    }
    block(attributeString);
}

2.文字前面加图片:

+(void)stringWithImage:(NSString *)currentString
             withBlock:(AttributedBlock)block;
+(void)stringWithImage:(NSString *)currentString
             withBlock:(AttributedBlock)block
{
    NSMutableAttributedString * attributeString = [[NSMutableAttributedString alloc]initWithString:currentString];
    NSTextAttachment *attach = [[NSTextAttachment alloc] init];
    attach.image = [UIImage imageNamed:@"需要添加的图片"];
    attach.bounds = CGRectMake(0, -3, 15, 15);
    NSAttributedString *attachString = [NSAttributedString attributedStringWithAttachment:attach];
    [attributeString insertAttributedString:attachString atIndex:0];
    block(attributeString);
}

3.显示价格的时候需要显示中划线:

/**中划线。价格*/
+(void)stringWithLine:(NSString *)currentString
            withBlock:(AttributedBlock)block;
+(void)stringWithLine:(NSString *)currentString
            withBlock:(AttributedBlock)block
{
    NSMutableAttributedString * attributeString = [[NSMutableAttributedString alloc]initWithString:currentString];
    NSInteger currentStringLength = currentString.length;
    [attributeString addAttribute:NSStrikethroughStyleAttributeName value:@(NSUnderlineStyleSingle) range:NSMakeRange(0, currentStringLength)];
    [attributeString addAttribute:NSStrikethroughColorAttributeName value:[UIColor lightGrayColor] range:NSMakeRange(0, currentStringLength)];
    block(attributeString);
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 1、截取字符串”20 | http://www.baidu.com”中,”|”字符前面和后面的数据,分别输出它们 ...
    强子ly阅读 8,199评论 8 46
  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,458评论 30 472
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,212评论 19 139
  • { 11、核心动画 需要签协议,但是系统帮签好 一、CABasicAnimation 1、创建基础动画对象 CAB...
    CYC666阅读 5,544评论 2 4
  • 一、背景信息楼主在香港上学,非永久居民,具有港澳通行证逗留签注,持中国护照。 今年暑假要去葡萄牙开会,会后计划去西...
    鼹鼠素素阅读 12,007评论 3 1

友情链接更多精彩内容