要想给UITextView 添加连接 我们要使用到 NSMutableAttributedString 给 UITextView的 attributedText 添加个连接 ,这是 长按会进入到UITextView的 delegate中去 我们只需要在delegate中实现连接就好了 在这里 如果长按的时间稍微长一点调用自己的一个 action
具体的实例如下:
UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(10, 100, 50, 100)];
textView.delegate = self;
textView.editable = NO;
[self.view addSubview:textView];
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:@"This is an example by test"];
[attributedString addAttribute:NSLinkAttributeName value:@"http://test" range:[[attributedString string] rangeOfString:@"test"]];
NSDictionary *linkAttributes = @{NSForegroundColorAttributeName: [UIColor greenColor],
NSUnderlineColorAttributeName: [UIColor lightGrayColor],
NSUnderlineStyleAttributeName: @(NSUnderlinePatternSolid)};
textView.linkTextAttributes = linkAttributes; // customizes the appearance of links
textView.attributedText = attributedString;
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange
{
// to do some things
return YES;
}
(单击好像是不可以的)