请问大家:使用UITextView添加链接 NSLinkAttributeName 时,有没有遇到过这种情况:点击区域实际是在设定范围内放大了(上方和左方为两个文字的宽或高,右边全部空白区域,如何解决? 谢谢!
代码如下:
UITextView *tipsTextView = [[UITextView alloc] initWithFrame:CGRectMake(vPadding, 5, infoBgImgView.width - vPadding * 2, infoBgImgView.height - 5 * 2)];
tipsTextView.editable = NO;
tipsTextView.selectable = YES;
tipsTextView.delegate = self;
tipsTextView.dataDetectorTypes = UIDataDetectorTypeLink;
NSMutableAttributedString *muAttributedStr = [[NSMutableAttributedString alloc] initWithString:@"点击链接范围是设定区域及其上方的一行对应的区域,左方的两个字,以及右边的空白区域。再加文字,点击区域会发生变化,请至点链接区域"];
//添加链接
NSURL * url = [NSURL URLWithString:@"http://www.baidu.com"];
// tipsTextView.selectedTextRange = NSMakeRange(muAttributedStr.length - 5, 5);
NSRange selectRange = NSMakeRange(muAttributedStr.length - 5, 5);
[muAttributedStr addAttribute:NSLinkAttributeName value:url range:selectRange];
// [tipsTextView addObserver:self forKeyPath:@"contentSize"options:NSKeyValueObservingOptionNew context:nil];
tipsTextView.attributedText = muAttributedStr;
[infoBgImgView addSubview:tipsTextView];