TTTAttributedLabel 是一個常用的富文本開源庫,支持各種屬性文本、數(shù)據(jù)探測器,鏈接等。
下面我們來看看它的用法。
TTTAttributedLabel 創(chuàng)建:
- (TTTAttributedLabel *)aLable
{
if (!_aLable)
{
_aLable = [[TTTAttributedLabel alloc] initWithFrame:CGRectMake(10, 80, SCREEN_WIDTH - 20, SCREEN_HEIGHT - 200)];
_aLable.backgroundColor = RGBCOLOR(246, 246, 246);
_aLable.lineBreakMode = NSLineBreakByWordWrapping;
_aLable.numberOfLines = 0;
_aLable.delegate = self;
_aLable.lineSpacing = 10;
//要放在`text`, with either `setText:` or `setText:afterInheritingLabelAttributesAndConfiguringWithBlock:前面才有效
_aLable.enabledTextCheckingTypes = NSTextCheckingTypePhoneNumber|NSTextCheckingTypeAddress|NSTextCheckingTypeLink;
//鏈接正常狀態(tài)文本屬性
_aLable.linkAttributes = @{NSForegroundColorAttributeName:[UIColor purpleColor],NSUnderlineStyleAttributeName:@(1)};
//鏈接高亮狀態(tài)文本屬性
_aLable.activeLinkAttributes = @{NSForegroundColorAttributeName:[UIColor blackColor],NSUnderlineStyleAttributeName:@(1)};
}
return _aLable;
}
注:enabledTextCheckingTypes
要放在text
, with either setText:
or setText:afterInheritingLabelAttributesAndConfiguringWithBlock:
前面才有效.
文本賦值
NSString *text = DaoXiang;
__block CGSize size;
[self.aLable setText:text afterInheritingLabelAttributesAndConfiguringWithBlock:^NSMutableAttributedString *(NSMutableAttributedString *mutableAttributedString) {
NSRange fontRange = [[mutableAttributedString string] rangeOfString:@"對這個世界如果你有太多的抱怨"
options:NSCaseInsensitiveSearch];
NSRange strokeColorRange1 = [[mutableAttributedString string] rangeOfString:@"跌倒了 就不敢繼續(xù)往前走"
options:NSCaseInsensitiveSearch];
NSRange strikeRange = [[mutableAttributedString string] rangeOfString:@"為什麼 人要這麼的脆弱 墮落"
options:NSCaseInsensitiveSearch];
NSRange fillColorRange = [[mutableAttributedString string] rangeOfString:@"請你打開電視看看"
options:NSCaseInsensitiveSearch];
NSRange shadowRange = [[mutableAttributedString string] rangeOfString:@"為生命在努力勇敢的走下去"
options:NSCaseInsensitiveSearch];
NSRange obliquenessRange = [[mutableAttributedString string] rangeOfString:@"還記得你說家是唯一的城堡"
options:NSCaseInsensitiveSearch];
UIEdgeInsets fillPadding = UIEdgeInsetsMake(0, 0, 0, 0);
// Core Text APIs use C functions without a direct bridge to UIFont. See Apple's "Core Text Programming Guide" to learn how to configure string attributes.
UIFont *boldSystemFont = [UIFont boldSystemFontOfSize:16];
CTFontRef font = CTFontCreateWithName((__bridge CFStringRef)boldSystemFont.fontName, boldSystemFont.pointSize, NULL);
if (font) {
{
//字體
[mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName
value:(__bridge id)font
range:fontRange];
//文字顏色
[mutableAttributedString addAttribute:(NSString *)kCTForegroundColorAttributeName
value:[UIColor redColor]
range:fontRange];
CFRelease(font);
}
{
//NSStrokeColorAttributeName設(shè)置文字描邊顏色,需要和NSStrokeWidthAttributeName設(shè)置描邊寬度,這樣就能使文字空心
//文字描邊顏色
[mutableAttributedString addAttribute:NSStrokeColorAttributeName
value:[UIColor blueColor]
range:strokeColorRange1];
//文字描邊寬度
[mutableAttributedString addAttribute:NSStrokeWidthAttributeName
value:@(2.0)
range:strokeColorRange1];
}
{
//刪除樣式
[mutableAttributedString addAttribute:kTTTStrikeOutAttributeName
value:@YES
range:strikeRange];
//加上下劃線
[mutableAttributedString addAttribute:NSUnderlineStyleAttributeName
value:[NSNumber numberWithInt:3]
range:strikeRange];
[mutableAttributedString addAttribute:NSUnderlineColorAttributeName
value:[UIColor greenColor]
range:strikeRange];
}
{
//背景色
[mutableAttributedString addAttribute:kTTTBackgroundFillColorAttributeName
value:[UIColor purpleColor]
range:fillColorRange];
//控制背景色范圍
[mutableAttributedString addAttribute:kTTTBackgroundFillPaddingAttributeName
value:[NSNumber valueWithUIEdgeInsets:fillPadding]
range:fillColorRange];
//控制背景色(文字邊框)的圓角
[mutableAttributedString addAttribute:kTTTBackgroundCornerRadiusAttributeName
value:@(4)
range:fillColorRange];
//文字邊框顏色
[mutableAttributedString addAttribute:kTTTBackgroundStrokeColorAttributeName
value:[UIColor purpleColor]
range:fillColorRange];
}
{
//無效
NSShadow *shadow = [[NSShadow alloc] init];
[shadow setShadowColor:[UIColor redColor]];
[shadow setShadowBlurRadius:4.0];
[shadow setShadowOffset:CGSizeMake(2, 2)];
//陰影
[mutableAttributedString addAttribute:NSShadowAttributeName
value:shadow
range:shadowRange];
}
{
//無效
//斜體
//NSObliquenessAttributeName 設(shè)置字體傾斜度,取值為 NSNumber(float),正值右傾,負值左傾
[mutableAttributedString addAttribute:NSObliquenessAttributeName
value:@(5.0)
range:obliquenessRange];
}
}
//高度計算
size = [TTTAttributedLabel sizeThatFitsAttributedString:mutableAttributedString
withConstraints:CGSizeMake(SCREEN_WIDTH - 20, CGFLOAT_MAX)
limitedToNumberOfLines:0];
return mutableAttributedString;
}];
NSRange boldRange1 = [text rangeOfString:@"隨著稻香河流繼續(xù)奔跑" options:NSCaseInsensitiveSearch];
[self.aLable addLinkToURL:[NSURL URLWithString:@"http://y.qq.com/portal/song/003aAYrm3GE0Ac.html"]
withRange:boldRange1];
//電話號碼可以自動識別,也可以這樣添加
// NSRange phoneRange = [text rangeOfString:phoneNum options:NSCaseInsensitiveSearch];
// [self.aLable addLinkToPhoneNumber:phoneNum withRange:phoneRange];
NSRange addressRange = [text rangeOfString:@"微微笑 小時候的夢我知道" options:NSCaseInsensitiveSearch];
[self.aLable addLinkToAddress:@{@"detailAdd":@"幸福街122號",@"lontitude":@"110.011111",@"latitude":@"30.1234"}
withRange:addressRange];
self.aLable.frame = CGRectMake(10, 0, SCREEN_WIDTH - 20, size.height);
[self addSubview:self.aLable];
點擊鏈接代理TTTAttributedLabelDelegate
數(shù)據(jù)探測器會自動探測到鏈接,郵件地址等。下面代理,就可以實現(xiàn)點擊鏈接的回調(diào)。
#pragma mark - TTTAttributedLabelDelegate
- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url
{
NSLog(@"linkClick");
[[UIApplication sharedApplication] openURL:url];
}
- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithAddress:(NSDictionary *)addressComponents
{
NSLog(@"addressClick");
NSLog(@"detailAdd:%@,lontitude:%f,latitude:%f",addressComponents[@"detailAdd"],
[addressComponents[@"lontitude"] floatValue],
[addressComponents[@"latitude"] floatValue]);
}
- (void)attributedLabel:(TTTAttributedLabel *)label
didSelectLinkWithPhoneNumber:(NSString *)phoneNumber
{
NSLog(@"phoneClick");
NSString *num = [NSString stringWithFormat:@"tel:%@",phoneNumber];
// NSString *num = [NSString stringWithFormat:@"telprompt://%@",number];
//而這個方法則打電話前先彈框 是否打電話 然后打完電話之后回到程序中 網(wǎng)上說這個方法可能不合法 無法通過審核
UIApplication *application = [UIApplication sharedApplication];
AppDelegate *delegate = (AppDelegate *)application.delegate;
UIWindow *window = delegate.window;
UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil
message:[NSString stringWithFormat:@"是否撥打 %@",phoneNumber]
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:@"取消"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction * _Nonnull action) {}];
UIAlertAction *actionDone = [UIAlertAction actionWithTitle:@"確定"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * _Nonnull action) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:num]]; //撥號
}];
[alert addAction:actionCancel];
[alert addAction:actionDone];
[window.rootViewController presentViewController:alert animated:YES completion:nil];
}
上一篇《YYText 庫學(xué)習(xí)總結(jié)》簡單介紹了YYText的用法,下面做一個對比:
功能|YYLabel|YYTextView| TTTAttributedLabel|備注
:---:|:---:|:---:|:---:
字體|??|??|??|
顏色|??|??|??|
描邊|??|??|??|
刪除樣式|??|??|??|
下劃線|??|??|??|
背景色|??|??|??|
陰影|??|??|?|
斜體|?|?|?|
圖片|??|??|?|TTT不支持
鏈接|??|??|??|
數(shù)據(jù)探測器|?|??|??|
注:數(shù)據(jù)探測器即自動檢測鏈接,電話,郵箱,地址等。