当用户点击textView中的链接时,整个链接应该高亮显示。但是,如果链接文本的样式不一致,则它只高亮显示链接文本中从第一个字符开始具有相同属性的部分。
以下是代码示例:
NSMutableAttributedString* someAttrString = [[NSMutableAttributedString alloc] initWithString:@"This style, that style"];
[someAttrString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:10] range:NSMakeRange(0, 11)];
[someAttrString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12] range:NSMakeRange(12, 8)];
[someAttrString addAttribute:NSLinkAttributeName value:@"http://www.apple.com" range:NSMakeRange(0, someAttrString.length)];
someUITextView.attributedText = someAttrString;
这件事有变通办法吗?我需要风格我的链接是一种特定的方式,而不是让它看起来很奇怪,当用户点击它。
第一个解决方案很简单:这将帮助您TTTAttributedLabel
第二种解决方案:
NSAttributedString*attributedString=[[NSAttributedSstring alloc]initWithString:@"www.google.com"属性:@{@"LINK_CUSTOM":@"www.google.com"}];[段落appendAttributedString:attributedString];
将UIMapGesture添加到UITextView
(void)text点击:(UIMapGestureRecognizer*)识别器{UITextView*textView=(UITextView*)识别器.view;
//点击在文本容器坐标中的位置
NSLayoutManager*layoutManager=textView.layoutManager;CGPoint位置=[识别器位置InView:textView];location.x-=textView.textContainerInset.left;location.y-=textView.textContainerInset.top;
NSU整数字符索引;characterIndex=[layoutManager characterIndexForPoint:locationinTextContainer:textView.textContainer插入点之间的距离分数:NULL];
if(characterIndex<textView.textStorage.length){
NSRange range; id value = [textView.attributedText attribute:@"LINK_CUSTOM" atIndex:characterIndex effectiveRange:&range];
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:[value字符串值]]];
} }