由于最近常用富文本, 在编辑一个富文本时需要操作很多的属性, 书写起来很不方便. 所以我将这些相关属性整理并使用链式方式将它简化了一下. 效果请看下面Demo.

项目工程 实现很简单, 我嘴太笨, 这里就不介绍了, 如有兴趣直接看源码吧. 同时可以通过cocoapods来使用它.

  pod 'SJAttributesFactory'

附关于富文本的一些属性.


上下图文效果:

上下图文.jpg上下图文.jpg
之前:

   // 文本字典NSDictionary *titleDict = @{NSFontAttributeName: [UIFont systemFontOfSize:fontSize],NSForegroundColorAttributeName: titleColor};NSDictionary *spacingDict = @{NSFontAttributeName: [UIFont systemFontOfSize:spacing]};// 图片文本NSTextAttachment *attachment = [[NSTextAttachment alloc] init];attachment.image = image;attachment.bounds = CGRectMake(0, 0, imageW, imageH);NSAttributedString *imageText = [NSAttributedString attributedStringWithAttachment:attachment];// 换行文本NSAttributedString *lineText = [[NSAttributedString alloc] initWithString:@"\n\n" attributes:spacingDict];// 按钮文字NSAttributedString *text = [[NSAttributedString alloc] initWithString:title attributes:titleDict];// 合并文字NSMutableAttributedString *attM = [[NSMutableAttributedString alloc] initWithAttributedString:imageText];[attM appendAttributedString:lineText];[attM appendAttributedString:text];

现在:

[SJAttributesFactory alteringStr:@"9999" task:^(SJAttributesFactory * _Nonnull worker) {worker.insertText(@"\n", 0).insertImage([UIImage imageNamed:@"sample2"], CGPointZero, CGSizeMake(50, 50), 0).lineSpacing(8) // 加点行间隔.alignment(NSTextAlignmentCenter).font([UIFont boldSystemFontOfSize:14]).fontColor([UIColor whiteColor]);}];

左缩进 + 右缩进

左缩进 + 右缩进.jpeg左缩进 + 右缩进.jpeg
之前:

    NSString *str = @"故事:可以解释为旧事、旧业、先例、典故等涵义,同时,也是文学体裁的一种,侧重于事情过程的描述,强调情节跌宕起伏,从而阐发道理或者价值观。";NSMutableAttributedString *attrM = [[NSMutableAttributedString alloc] initWithString:str];[attrM addAttribute:NSFontAttributeNamevalue:[UIFont boldSystemFontOfSize:14]range:NSMakeRange(0, 3)];NSMutableParagraphStyle *style = [NSMutableParagraphStyle new];style.firstLineHeadIndent = 8;style.headIndent = [[attrM attributedSubstringFromRange:NSMakeRange(0, 3)]boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, CGFLOAT_MAX)options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeadingcontext:nil].size.width + style.firstLineHeadIndent;style.tailIndent = -8;[attrM addAttribute:NSParagraphStyleAttributeNamevalue:stylerange:NSMakeRange(0, str.length)];

现在:

    [SJAttributesFactory alteringStr:@"故事:可以解释为旧事、旧业、先例、典故等涵义,同时,也是文学体裁的一种,侧重于事情过程的描述,强调情节跌宕起伏,从而阐发道理或者价值观。" task:^(SJAttributesFactory * _Nonnull worker) {worker.nextFont([UIFont boldSystemFontOfSize:14]).range(NSMakeRange(0, 3));CGFloat startW = worker.width(NSMakeRange(0, 3));worker.firstLineHeadIndent(8).headIndent(startW + 8).tailIndent(-8);       }];

下划线 + 删除线

下划线 + 删除线.jpg下划线 + 删除线.jpg

之前:

    NSString *price = @"$ 999";NSMutableAttributedString *attrM = [[NSMutableAttributedString alloc] initWithString:price];NSRange range = NSMakeRange(0, price.length);[attrM addAttribute:NSFontAttributeNamevalue:[UIFont systemFontOfSize:40]range:range];[attrM addAttribute:NSUnderlineStyleAttributeNamevalue:@(NSUnderlineByWord | NSUnderlinePatternSolid | NSUnderlineStyleDouble)range:range];[attrM addAttribute:NSUnderlineColorAttributeNamevalue:[UIColor yellowColor]range:range];[attrM addAttribute:NSStrikethroughStyleAttributeNamevalue:@(NSUnderlineByWord | NSUnderlinePatternSolid | NSUnderlineStyleDouble)range:range];[attrM addAttribute:NSStrikethroughColorAttributeNamevalue:[UIColor redColor]range:range];

现在:

    [SJAttributesFactory alteringStr:@"$ 999" task:^(SJAttributesFactory * _Nonnull worker) {worker.font([UIFont systemFontOfSize:40]);worker.underline(NSUnderlineByWord | NSUnderlinePatternSolid | NSUnderlineStyleDouble, [UIColor yellowColor]).strikethrough(NSUnderlineByWord | NSUnderlinePatternSolid | NSUnderlineStyleDouble, [UIColor redColor]);}];

Other

ex.gifex.gif