TTTAttributedLabel可以滿足在一段文本內(nèi)容中展示網(wǎng)址鏈接:
TTTAttributedLabel *label = [TTTAttributedLabel alloc] initWithFrame:frame];
label.delegate = self;
label.enabledTextCheckingTypes = NSTextCheckingTypeLink;
label.numberOfLines = 0;
[self.view addSubView:label];
當(dāng)然你也可以更改鏈接的樣式:
NSMutableDictionary *linkAttributes = [NSMutableDictionary dictionary];
[linkAttributes setValue:[NSNumber numberWithBool:NO] forKey:(NSString *)kCTUnderlineStyleAttributeName];
[linkAttributes setValue:(__bridge id)ciweiLinkColor.CGColor forKey:(NSString *)kCTForegroundColorAttributeName];
label.linkAttributes = linkAttributes;
只需要實(shí)現(xiàn)代理方法:
- (void)attributedLabel:(TTTAttributedLabel *)label
didSelectLinkWithURL:(NSURL *)url{
//這里可以對(duì)點(diǎn)擊的url進(jìn)行操作
}
在很多的社交類軟件包括微信、微博等軟件中常常會(huì)遇到下面這樣的需求:
在帖子評(píng)論中,如果這個(gè)評(píng)論被回復(fù)了(子回復(fù)),子回復(fù)中的昵稱可點(diǎn)擊并且點(diǎn)擊后一般是去這個(gè)用戶的個(gè)人主頁面:
NSString *string = [NSString stringWithFormat:@"%@ 回復(fù) %@:%@",reply.nickname,reply.toNickname,reply.content];
label.text = string;
NSRange range = NSMakeRange(0, reply.nickname.length);
[self addLinkToURL:[NSURL URLWithString:[NSString stringWithFormat:@"scheme://?type=1&business_id=%@",reply.userId]] withRange:range];
NSRange toRange = NSMakeRange(reply.nickname.length+4, reply.toNickname.length);
[self addLinkToURL:[NSURL URLWithString:[NSString stringWithFormat:@"scheme://?type=1&business_id=%@",reply.toUserId]] withRange:toRange];
因?yàn)檫@里用到時(shí)通過點(diǎn)擊連接跳轉(zhuǎn)到app內(nèi)部的一個(gè)現(xiàn)有頁面,所以這里我傳入的鏈接是Scheme Url,在處理代理方法的時(shí)候:
- (void)attributedLabel:(TTTAttributedLabel *)label
didSelectLinkWithURL:(NSURL *)url{
NSString *urlStr = [url absoluteString];
if([urlStr hasPrefix:@"scheme"]){
[[UIApplication sharedApplication] openURL:url];
}else{
//這里可以對(duì)點(diǎn)擊的url進(jìn)行操作
}
}
然后在AppDelegate中處理點(diǎn)擊昵稱的方法,這里會(huì)把昵稱的超鏈接傳入url:
-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
}
其實(shí)大可不必這么費(fèi)勁,完全可以將其分成兩部分控件:第一部分展示昵稱信息,給其賦予點(diǎn)擊事件;第二部分展示回復(fù)文本內(nèi)容。
但是考慮到,如果是從一個(gè)分享網(wǎng)頁中點(diǎn)擊昵稱,需要app作出響應(yīng),是的自己的app更加靈活;或者是在回復(fù)內(nèi)容中需要做@某個(gè)用戶的操作,為了以后的擴(kuò)展就這么干了?!救收咭娙省?/p>
點(diǎn)擊帖子回復(fù)對(duì)其進(jìn)行回復(fù)操作:
對(duì)label的父控件添加點(diǎn)擊手勢(shì):
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(subReplyClick:)];
[view addGestureRecognizer:tap];
但是運(yùn)行代碼之后發(fā)現(xiàn)TTTAttributedLabel的父控件添加手勢(shì)之后,其代理方法*attributedLabel:(TTTAttributedLabel *)label
didSelectLinkWithURL:(NSURL )url不再被執(zhí)行。
解決手勢(shì)沖突:
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(subReplyClick:)];
tap.delegate = self;
[view addGestureRecognizer:tap];
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
if ([touch.view isKindOfClass:[TTTAttributedLabel class]]){
TTTAttributedLabel *label = (TTTAttributedLabel *)touch.view;
if ([label containslinkAtPoint:[touch locationInView:label]]){
return NO;
}else{
return YES;
}
}else{
return YES;
}
}
然后現(xiàn)在就應(yīng)該是可以了?。?!TTTAttributedLabel是一個(gè)很強(qiáng)大控件,其他的使用方法以后再說吧。。。