实现:
给不同文字设置不同的颜色、字体大小、背景颜色
给文字添加点击事件
给文字添加长按事件
//
// ViewController.m
// TTTAttributedLabelTest
//
// Created by chj on 2017/5/6.
// Copyright © 2017年 chj. All rights reserved.
//
#import "ViewController.h"
#import "TTTAttributedLabel.h"
@interface ViewController ()<TTTAttributedLabelDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSString *text=@"弱者普遍易怒如虎,而且容易暴怒。强者通常平静如水,并且相对平和。一个内心不强大的人,自然内心不够平静。内心不平静的人,处处是风浪。再小的事,都会被无限放大。一个内心不强大的人,心中永远缺乏安全感 https://github.com/TTTAttributedLabel/TTTAttributedLabel 15112345678 2017-05-06 天安门";
TTTAttributedLabel *label = [[TTTAttributedLabel alloc] initWithFrame:CGRectMake(0, 120, self.view.frame.size.width, 200)];
label.font = [UIFont systemFontOfSize:14];
label.numberOfLines = 0;
[self.view addSubview:label];
label.text=text;
label.delegate=self;
//设置行间距
label.lineSpacing = 8;
//可自动识别url,显示为蓝色+下划线
label.enabledTextCheckingTypes = NSTextCheckingTypeLink;
//此属性可以不显示下划线,点击的颜色默认为红色
label.linkAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:NO],(NSString *)kCTUnderlineStyleAttributeName,nil];
//此属性可以改变点击的颜色
label.activeLinkAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor purpleColor],(NSString *)kCTForegroundColorAttributeName,nil];
//设置需要点击的文字的颜色大小
[label setText:text afterInheritingLabelAttributesAndConfiguringWithBlock:^NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString) {
//得到需要点击的文字的位置
NSRange selRange=[text rangeOfString:@"强者通常平静如水"];
//设定可点击文字的的大小
UIFont *selFont=[UIFont systemFontOfSize:14];
CTFontRef selFontRef = CTFontCreateWithName((__bridge CFStringRef)selFont.fontName, selFont.pointSize, NULL);
//设置可点击文本的大小
[mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName value:(__bridge id)selFontRef range:selRange];
//设置可点击文本的颜色
[mutableAttributedString addAttribute:(NSString*)kCTForegroundColorAttributeName value:(id)[[UIColor blueColor] CGColor] range:selRange];
//设置可点击文本的背景颜色
[mutableAttributedString addAttribute:(NSString*)kCTBackgroundColorAttributeName value:(id)[[UIColor redColor] CGColor] range:selRange];
CFRelease(selFontRef);
//得到需要点击的文字的位置
NSRange selRange1=[text rangeOfString:@"一个内心不强大的人,心中永远缺乏安全感"];
//设定可点击文字的的大小
UIFont *selFont1=[UIFont systemFontOfSize:14];
CTFontRef selFontRef1 = CTFontCreateWithName((__bridge CFStringRef)selFont1.fontName, selFont1.pointSize, NULL);
//设置可点击文本的大小
[mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName value:(__bridge id)selFontRef1 range:selRange1];
//设置可点击文本的颜色
[mutableAttributedString addAttribute:(NSString*)kCTForegroundColorAttributeName value:(id)[[UIColor blueColor] CGColor] range:selRange1];
//设置可点击文本的背景颜色
//[mutableAttributedString addAttribute:(NSString*)kCTBackgroundColorAttributeName value:(id)[[UIColor redColor] CGColor] range:selRange1];
CFRelease(selFontRef1);
return mutableAttributedString;
}];
//给 强者通常平静如水 添加点击事件
NSRange selRange=[text rangeOfString:@"强者通常平静如水"];
[label addLinkToTransitInformation:@{@"select":@"强者通常平静如水"} withRange:selRange];
//给 强者通常平静如水 添加点击事件
NSRange selRange1=[text rangeOfString:@"一个内心不强大的人,心中永远缺乏安全感"];
[label addLinkToTransitInformation:@{@"select":@"一个内心不强大的人,心中永远缺乏安全感"} withRange:selRange1];
//给 电话号码 添加点击事件
NSRange telRange=[text rangeOfString:@"15112345678"];
[label addLinkToPhoneNumber:@"15112345678" withRange:telRange];
//给 时间 添加点击事件
NSRange dateRange=[text rangeOfString:@"2017-05-06"];
[label addLinkToDate:[NSDate date] withRange:dateRange];
//给 天安门 添加点击事件
NSRange addressRange=[text rangeOfString:@"天安门"];
[label addLinkToAddress:@{@"address":@"天安门",@"longitude":@"116.2354",@"latitude":@"38.2145"} withRange:addressRange];
}
#pragma TTTAttributedLabel Delegate
//文字的点击事件
- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithTransitInformation:(NSDictionary *)components {
NSLog(@"didSelectLinkWithTransitInformation :%@",components);
}
- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url {
NSLog(@"didSelectLinkWithURL :%@",url);
}
- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithDate:(NSDate *)date {
NSLog(@"didSelectLinkWithDate :%@",date);
}
- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithAddress:(NSDictionary *)addressComponents {
NSLog(@"didSelectLinkWithAddress :%@",addressComponents);
}
- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithPhoneNumber:(NSString *)phoneNumber {
NSLog(@"didSelectLinkWithPhoneNumber :%@",phoneNumber);
}
//文字的长按事件
- (void)attributedLabel:(TTTAttributedLabel *)label didLongPressLinkWithURL:(NSURL *)url atPoint:(CGPoint)point {
NSLog(@"didLongPressLinkWithURL :%@",url);
}
- (void)attributedLabel:(TTTAttributedLabel *)label didLongPressLinkWithDate:(NSDate *)date atPoint:(CGPoint)point {
NSLog(@"didLongPressLinkWithDate :%@",date);
}
- (void)attributedLabel:(TTTAttributedLabel *)label didLongPressLinkWithAddress:(NSDictionary *)addressComponents atPoint:(CGPoint)point {
NSLog(@"didLongPressLinkWithAddress :%@",addressComponents);
}
- (void)attributedLabel:(TTTAttributedLabel *)label didLongPressLinkWithPhoneNumber:(NSString *)phoneNumber atPoint:(CGPoint)point {
NSLog(@"didLongPressLinkWithPhoneNumber :%@",phoneNumber);
}
- (void)attributedLabel:(TTTAttributedLabel *)label didLongPressLinkWithTransitInformation:(NSDictionary *)components atPoint:(CGPoint)point {
NSLog(@"didLongPressLinkWithTransitInformation :%@",components);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end