由于最近常用富文本, 在编辑一个富文本时需要操作很多的属性, 书写起来很不方便. 所以我将这些相关属性整理并使用链式方式将它简化了一下. 效果请看下面Demo.
项目地址在文章底部(实现很简单, 我嘴太笨, 这里就不介绍了, 如有兴趣直接看源码吧). 同时可以通过
来使用它.
- cocoapods
- pod 'SJAttributesFactory'
另外关于富文本的属性介绍, 请查看:
www.jianshu.com/p/ebbcfc24f…
之前:
- // 文本字典
- 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 alterStr:@"9999" block:^(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]);
- }];
之前:
- NSString *str = @"故事:可以解释为旧事、旧业、先例、典故等涵义,同时,也是文学体裁的一种,侧重于事情过程的描述,强调情节跌宕起伏,从而阐发道理或者价值观。";
- NSMutableAttributedString *attrM = [[NSMutableAttributedString alloc] initWithString:str];
- [attrM addAttribute:NSFontAttributeName
- value:[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 | NSStringDrawingUsesFontLeading
- context:nil].size.width + style.firstLineHeadIndent;
- style.tailIndent = -8;
- [attrM addAttribute:NSParagraphStyleAttributeName
- value:style
- range:NSMakeRange(0, str.length)];
现在:
- [SJAttributesFactory alterStr:@"故事:可以解释为旧事、旧业、先例、典故等涵义,同时,也是文学体裁的一种,侧重于事情过程的描述,强调情节跌宕起伏,从而阐发道理或者价值观。" block:^(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);
- }];
之前:
- NSString *price = @"$ 999";
- NSMutableAttributedString *attrM = [[NSMutableAttributedString alloc] initWithString:price];
- NSRange range = NSMakeRange(0, price.length);
- [attrM addAttribute:NSFontAttributeName
- value:[UIFont systemFontOfSize:40]
- range:range];
- [attrM addAttribute:NSUnderlineStyleAttributeName
- value:@(NSUnderlineByWord | NSUnderlinePatternSolid | NSUnderlineStyleDouble)
- range:range];
- [attrM addAttribute:NSUnderlineColorAttributeName
- value:[UIColor yellowColor]
- range:range];
- [attrM addAttribute:NSStrikethroughStyleAttributeName
- value:@(NSUnderlineByWord | NSUnderlinePatternSolid | NSUnderlineStyleDouble)
- range:range];
- [attrM addAttribute:NSStrikethroughColorAttributeName
- value:[UIColor redColor]
- range:range];
现在:
- [SJAttributesFactory alterStr:@"$ 999" block:^(SJAttributesFactory * _Nonnull worker) {
- worker.font([UIFont systemFontOfSize:40]);
- worker.underline(NSUnderlineByWord | NSUnderlinePatternSolid | NSUnderlineStyleDouble, [UIColor yellowColor]).strikethrough(NSUnderlineByWord | NSUnderlinePatternSolid | NSUnderlineStyleDouble, [UIColor redColor]);
- }];
Address: github.com/changsanjia…
来源: https://juejin.im/post/5a06a4456fb9a0451463834b