實現(xiàn):
給不同文字設(shè)置不同的顏色、字體大小、背景顏色
給文字添加點擊事件
給文字添加長按事件
BC4FAA3E-43A2-4E5A-B066-20369368D280.png
//
// 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=@"弱者普遍易怒如虎铸董,而且容易暴怒。強(qiáng)者通常平靜如水,并且相對平和习贫。一個內(nèi)心不強(qiáng)大的人,自然內(nèi)心不夠平靜千元。內(nèi)心不平靜的人苫昌,處處是風(fēng)浪。再小的事幸海,都會被無限放大祟身。一個內(nèi)心不強(qiáng)大的人,心中永遠(yuǎn)缺乏安全感 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;
//設(shè)置行間距
label.lineSpacing = 8;
//可自動識別url物独,顯示為藍(lán)色+下劃線
label.enabledTextCheckingTypes = NSTextCheckingTypeLink;
//此屬性可以不顯示下劃線袜硫,點擊的顏色默認(rèn)為紅色
label.linkAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:NO],(NSString *)kCTUnderlineStyleAttributeName,nil];
//此屬性可以改變點擊的顏色
label.activeLinkAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor purpleColor],(NSString *)kCTForegroundColorAttributeName,nil];
//設(shè)置需要點擊的文字的顏色大小
[label setText:text afterInheritingLabelAttributesAndConfiguringWithBlock:^NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString) {
//得到需要點擊的文字的位置
NSRange selRange=[text rangeOfString:@"強(qiáng)者通常平靜如水"];
//設(shè)定可點擊文字的的大小
UIFont *selFont=[UIFont systemFontOfSize:14];
CTFontRef selFontRef = CTFontCreateWithName((__bridge CFStringRef)selFont.fontName, selFont.pointSize, NULL);
//設(shè)置可點擊文本的大小
[mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName value:(__bridge id)selFontRef range:selRange];
//設(shè)置可點擊文本的顏色
[mutableAttributedString addAttribute:(NSString*)kCTForegroundColorAttributeName value:(id)[[UIColor blueColor] CGColor] range:selRange];
//設(shè)置可點擊文本的背景顏色
[mutableAttributedString addAttribute:(NSString*)kCTBackgroundColorAttributeName value:(id)[[UIColor redColor] CGColor] range:selRange];
CFRelease(selFontRef);
//得到需要點擊的文字的位置
NSRange selRange1=[text rangeOfString:@"一個內(nèi)心不強(qiáng)大的人,心中永遠(yuǎn)缺乏安全感"];
//設(shè)定可點擊文字的的大小
UIFont *selFont1=[UIFont systemFontOfSize:14];
CTFontRef selFontRef1 = CTFontCreateWithName((__bridge CFStringRef)selFont1.fontName, selFont1.pointSize, NULL);
//設(shè)置可點擊文本的大小
[mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName value:(__bridge id)selFontRef1 range:selRange1];
//設(shè)置可點擊文本的顏色
[mutableAttributedString addAttribute:(NSString*)kCTForegroundColorAttributeName value:(id)[[UIColor blueColor] CGColor] range:selRange1];
//設(shè)置可點擊文本的背景顏色
//[mutableAttributedString addAttribute:(NSString*)kCTBackgroundColorAttributeName value:(id)[[UIColor redColor] CGColor] range:selRange1];
CFRelease(selFontRef1);
return mutableAttributedString;
}];
//給 強(qiáng)者通常平靜如水 添加點擊事件
NSRange selRange=[text rangeOfString:@"強(qiáng)者通常平靜如水"];
[label addLinkToTransitInformation:@{@"select":@"強(qiáng)者通常平靜如水"} withRange:selRange];
//給 強(qiáng)者通常平靜如水 添加點擊事件
NSRange selRange1=[text rangeOfString:@"一個內(nèi)心不強(qiáng)大的人挡篓,心中永遠(yuǎn)缺乏安全感"];
[label addLinkToTransitInformation:@{@"select":@"一個內(nèi)心不強(qiáng)大的人婉陷,心中永遠(yuǎn)缺乏安全感"} 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