参考文档1
参考文档2
[参考文档3]( [mutableAttributedString addAttribute:(NSString *)kCTForegroundColorAttributeName
value:[UIColor redColor]
range:fontRange];)
之前写过关于富文本的处理,确实略显麻烦文章链接,相比而言,YYText使用简单且功能强大。当然汲及到富文本链接,多用TTTAttributedLabel。具体项目中MLEmojiLabel涉及表情处理的话感觉更方便一些。
YYLabel 设置字体样式更方便简洁
TTT 及其子类 ML 设置字体样式等感觉代码有点烦琐,处理链接方便,且可以很方便准确的处理给定文字的高度处理。
YYText
- 基础用法
#import "ViewController.h"
#import <YYText.h>
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
#pragma mark - 基本用法
//基本用法和UILabel相似
YYLabel *label = [[YYLabel alloc]init];
label.frame = CGRectMake(0, 0, 200, 200);
label.center = self.view.center;
label.numberOfLines = 2;
[self.view addSubview:label];
#pragma mark - 属性文本
NSMutableAttributedString *str = [[NSMutableAttributedString alloc]initWithString:@"Change the fucking world"];
str.yy_font = [UIFont systemFontOfSize:20];
str.yy_color = [UIColor orangeColor];
[str yy_setColor:[UIColor greenColor] range:NSMakeRange(7, 4)];
//*******也可以直接搜索相应文字改变属性******//
//NSRange range= [[text string] rangeOfString:@"对这个世界如果你有太多的抱怨" options:NSCaseInsensitiveSearch];
//行间距
str.yy_lineSpacing =10;
//文字描边(空心字)默认黑色,必须设置width
[text yy_setStrokeColor:[UIColor orangeColor] ];
[text yy_setStrokeWidth:@(2) ];
YYTextDecoration *decoration =
[YYTextDecoration decorationWithStyle:YYTextLineStyleSingle
width:@(1)
color:[UIColor blueColor]];
//删除样式
[text yy_setTextStrikethrough:decoration range:range2];
//下划线
[text yy_setTextUnderline:decoration range:range2];
//阴影
NSShadow *shadow = [[NSShadow alloc]init];
shadow.shadowColor = [UIColor grayColor];
shadow.shadowOffset = CGSizeMake(1, 1.5);
str.yy_shadow = shadow;
//文本内阴影
YYTextShadow *shadow = [YYTextShadow new];
shadow.color = [UIColor redColor];
shadow.offset = CGSizeMake(0, 2);
shadow.radius = 1;
[str yy_setTextInnerShadow:shadow ];
//首行缩进
str.yy_firstLineHeadIndent = 10;
//背景框
//多行显示时效果会不太如愿
str.yy_textBorder = [ YYTextBorder borderWithLineStyle:YYTextLineStylePatternCircleDot
lineWidth:1
strokeColor:[UIColor greenColor]];
str.yy_textBorder.cornerRadius = 3;
str.yy_textBorder.insets = UIEdgeInsetsMake(0, -2, 0, -2);
#pragma mark - 高亮显示
[str yy_setTextHighlightRange:range
color:[UIColorr redColor]
backgroundColor:[UIColor greenColor]
tapAction:^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect){
//点击事件
}];
//一定要放在最后
label.attributedText = str;
}
- YYTextRubyAnnotation
NSMutableAttributedString *one = [[NSMutableAttributedString alloc] initWithString:@"这是用汉语写的一段文字。"];
one.yy_font = [UIFont boldSystemFontOfSize:30];
YYTextRubyAnnotation *ruby;
ruby = [YYTextRubyAnnotation new];
ruby.textBefore = @"hàn yŭ";
[one yy_setTextRubyAnnotation:ruby range:[one.string rangeOfString:@"汉语"]];
ruby = [YYTextRubyAnnotation new];
ruby.textBefore = @"wén";
[one yy_setTextRubyAnnotation:ruby range:[one.string rangeOfString:@"文"]];
ruby = [YYTextRubyAnnotation new];
ruby.textBefore = @"zì";
ruby.alignment = kCTRubyAlignmentCenter;
[one yy_setTextRubyAnnotation:ruby range:[one.string rangeOfString:@"字"]];
YYLabel *label = [YYLabel new];
label.backgroundColor = [UIColor whiteColor];
label.attributedText = one;
label.frame = CGRectMake(0, 10, self.view.frame.size.width, 200);
label.textAlignment = NSTextAlignmentCenter;
label.textVerticalAlignment = YYTextVerticalAlignmentCenter;
label.numberOfLines = 1;
[self.view addSubview:label];
TTTAttributed
-(void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self.view addSubview:self.label];
//也可以是NSString字符串
NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:@"注意注意注意注意注意注意"];
__block CGSize size;
[self.label setText:str afterInheritingLabelAttributesAndConfiguringWithBlock:^NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString) {
//文字边框颜色
NSRange range ;
[mutableAttributedString addAttribute:kTTTBackgroundStrokeColorAttributeName
value:[UIColor purpleColor]
range:range];
//删除样式
[mutableAttributedString addAttribute:kTTTStrikeOutAttributeName
value:@YES
range:range];
[mutableAttributedString addAttribute:(NSString *)kCTForegroundColorAttributeName
value:[UIColor redColor]
range:range];
//高度计算
size = [TTTAttributedLabel sizeThatFitsAttributedString:mutableAttributedString
withConstraints:CGSizeMake(kWidth, CGFLOAT_MAX)
limitedToNumberOfLines:0];
return mutableAttributedString;
}];
NSRange boldRange1 = [str rangeOfString:@"测试" options:NSCaseInsensitiveSearch];
[self.label addLinkToURL:[NSURL URLWithString:@"http://www.taobao.com"]
withRange:boldRange1];
//电话号码可以自动识别,也可以这样添加
//NSRange phoneRange = [text rangeOfString:phoneNum options:NSCaseInsensitiveSearch];
//[self.label addLinkToPhoneNumber:phoneNum withRange:phoneRange];
self.label.frame = CGRectMake(0, 30, kWidth, size.height);
}
- (TTTAttributedLabel*)label{
if (!_label) {
_label = [[TTTAttributedLabel alloc]initWithFrame:CGRectZero];
_label.lineBreakMode = NSLineBreakByTruncatingHead;
_label.numberOfLines = 0;
//_label.delegate = self;
_label.lineSpacing = 10;
/*
要放在`text`,
`setText:`
`setText:afterInheritingLabelAttributesAndConfiguringWithBlock:前面才有效
*/
_label.enabledTextCheckingTypes = NSTextCheckingTypePhoneNumber|NSTextCheckingTypeAddress|NSTextCheckingTypeLink;
//链接正常状态文本属性
_label.linkAttributes = @{NSForegroundColorAttributeName:[UIColor purpleColor],NSUnderlineStyleAttributeName:@(1)};
//链接高亮状态文本属性
_label.activeLinkAttributes = @{NSForegroundColorAttributeName:[UIColor redColor],NSUnderlineStyleAttributeName:@(1)};
}
return _label;
}
空心字
文本内阴影
文本阴影
背景框
网友评论