YGRichText
链式设置富文本! 可以自由组合成对应的富文本效果,省去多个方法的定义。
导入
支持 pod 导入
pod 'YGRichText'
或者手动导入 YGRichText
文件夹下的类。
使用
导入头文件
#import "YGRichText.h"
代码中调用
textView.attributedText = [@"hello world, this is rich text test~" yg_makeAttributed:^(YGAttributedMaker *make) {
make.font([UIFont systemFontOfSize:17]).allRange();
make.foregroundColor(UIColor.redColor).allRange();
make.strikethroughStyle(4).allRange();
make.underlineStyle(4).underlineColor(UIColor.blueColor).allRange();
make.strokeWidth(4).strokeColor(UIColor.blackColor).allRange();
make.textAlignment(NSTextAlignmentCenter).kern(10).lineSpacing(10).lineBreakMode(NSLineBreakByCharWrapping).allRange();
make.appendString(self.string);
}];
注意:每个属性设置结束后,请用 allRange()
或 inRange()
结束,这样才能生效!!!
插入图片
label.attributedText = [@" 会员" yg_makeAttributed:^(YGAttributedMaker *make) {
make.insertImage([UIImage imageNamed:@"image_name"], CGRectMake(0, -1, 12, 10), 0);
}];
由于图文富文本是直接调用 insertAttributedString:atIndex:
形式的,可以归为富文本拼接,无需调用 allRange()
或 inRange()
即可生效。
说明
可能存在的问题:相同的 range 分开设置 lineSpacing、textAlignment、lineBreakMode
,如下:
make.textAlignment(NSTextAlignmentCenter).allRange();
make.lineSpacing(10).allRange();
最终只有最后写入的代码会生效,例如上面的只有 lineSpacing
生效,覆盖了上方的 textAlignment
的效果。如果需要同时设置 lineSpacing、textAlignment、lineBreakMode
,需要在相同的 range 中调用:
make.textAlignment(NSTextAlignmentCenter).lineSpacing(10).lineBreakMode(NSLineBreakByCharWrapping).allRange();
联系
使用过程中如有问题,欢迎提交 issue。 作者简书