NSAttributedString是Objective-C中的属性字符串类,GitHub上也有很多第三方,用得较多的是TTTAttributedLabel,这里给大家介绍一下系统NSAttributedString类来实现富文本,并可实现点击事件,同时点击事件可携带参数。
因为要做点击事件,所以我们用UITextView,首先声明一个UITextView属性:
@property (nonatomic, strong) UITextView *textView;
NSAttributedString中NSFontAttributeName是用来设置文字字体的,有很多可以设置的属性,这里介绍一些常用的,其他的用到的话修改一下key-value就可以了:
//NSFontAttributeName:文字字体
- (void)setAttributeCorlorAndSize{
_textView = [[UITextView alloc]initWithFrame:CGRectMake(0, 30, SCREEN_WIDTH, 100)];
_textView.editable = NO;
_textView.selectable = NO;
_textView.scrollEnabled = NO;
_textView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
[self.view addSubview:_textView];
//初始化属性字符串
NSMutableAttributedString * aAttributedString = [[NSMutableAttributedString alloc] initWithString:@"富文本:文字颜色 字体大小 背景色 下划线 空心 点击事件"];
//文字颜色
[aAttributedString addAttribute:NSForegroundColorAttributeName
value:[UIColor redColor]
range:NSMakeRange(4, 4)];
//文字大小
[aAttributedString addAttribute:NSFontAttributeName
value:[UIFont systemFontOfSize:18]
range:NSMakeRange(9, 4)];
//文字背景色
[aAttributedString addAttribute:NSBackgroundColorAttributeName
value:[UIColor blueColor]
range:NSMakeRange(14, 3)];
//下划线 NSUnderlineColorAttributeName设置下划线颜色
[aAttributedString addAttribute:NSUnderlineStyleAttributeName
value:[NSNumber numberWithInt:1]
range:NSMakeRange(18, 3)];
//空心
[aAttributedString addAttribute:NSStrokeWidthAttributeName
value:[NSNumber numberWithInt:3]
range:NSMakeRange(22, 2)];
//点击事件携带一个NSInteger类型,值为110的参数
[aAttributedString addAttribute:@"tapID" value:[NSNumber numberWithInteger:110] range:NSMakeRange(25, 4)];
[_textView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(textTapped:)]];
NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
//行间距
paragraphStyle.lineSpacing = 15.0;
[aAttributedString addAttribute:NSParagraphStyleAttributeName
value:paragraphStyle
range:NSMakeRange(0, aAttributedString.length)];
_textView.attributedText = aAttributedString;
}
点击事件实现:
-(void)textTapped:(UITapGestureRecognizer*)recognizer{
NSLayoutManager *layoutManager = _textView.layoutManager;
CGPoint location = [recognizer locationInView:_textView];
location.x -= _textView.textContainerInset.left;
location.y -= _textView.textContainerInset.top;
NSUInteger characterIndex = [layoutManager characterIndexForPoint:location
inTextContainer:_textView.textContainer
fractionOfDistanceBetweenInsertionPoints:NULL];
if(characterIndex < _textView.textStorage.length){
NSRange range;
id number = [_textView.attributedText attribute:@"tapID" atIndex:characterIndex effectiveRange:&range];
NSNumber *value_id = number;
NSLog(@"%@", value_id);
}
}
NSParagraphStyleAttributeName用来设置段落样式(字符串通过“\n”进行分段,此设置必须在lable.numberOfLines = 0时有效,value通过NSMutableParagraphStyle设置,它有以下属性)
- (void)setAttributeParagraph{
UILabel *label2 = [[UILabel alloc]initWithFrame:CGRectMake(0, 130, SCREEN_WIDTH, 200)];
label2.numberOfLines = 0;
[self.view addSubview:label2];
NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc] initWithString:@"NSParagraphStyleAttributeName 段落样式(字符串通过“\n”进行分段,此设置必须在lable.numberOfLines = 0时有效,value通过NSMutableParagraphStyle设置,它有以下属性)"];
//段落样式
NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] init];
//段落间距
paragraphStyle.paragraphSpacing = 20.0;
[attributedString addAttribute:NSParagraphStyleAttributeName
value:paragraphStyle
range:NSMakeRange(0, attributedString.length)];
label2.attributedText = attributedString;
}
效果图,点击事件就不演示了,但点击“点击事件”四个字的时候,控制台打印所携带的参数110。