当用户点击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;
(即使我想在视觉上展示虫子的样子,我也没有发布此图片的声誉)
这件事有解决方法吗?我需要设置我的链接样式是一种特定的方式,当用户点击它时不会让它看起来很奇怪。
答案 0 :(得分:0)
第一个解决方案很简单:这将对您有所帮助TTTAttributedLabel
第二个解决方案:
NSAttributedString * attributedString = [[NSAttributedString alloc] initWithString:@“www.google.com”属性:@ {@“LINK_CUSTOM”: @“www.google.com”}]; [段 appendAttributedString:attributedString];
将UITapGesture添加到UITextView
(void)textTapped:(UITapGestureRecognizer *)识别器{ UITextView * textView =(UITextView *)recognizer.view;
//在文本容器坐标中点击的位置
NSLayoutManager * layoutManager = textView.layoutManager; CGPoint location = [识别器locationInView:textView]; location.x - = textView.textContainerInset.left; location.y - = textView.textContainerInset.top;
NSUInteger characterIndex; characterIndex = [layoutManager characterIndexForPoint:location inTextContainer:textView.textContainer fractionOfDistanceBetweenInsertionPoints: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 stringValue的]]];
} }