问题
@interface ViewController ()<UITextViewDelegate>
- (void)viewDidLoad
{
[super viewDidLoad];
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"www.google.com"];
NSDictionary *linkDic = @{ NSLinkAttributeName : [NSURL URLWithString:@"http://www.google.com"] };
[str setAttributes:linkDic range:[[str string] rangeOfString:@"www.google.com"]];
_textView.attributedText = str;
}
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange
{
NSLog(@"=============%@",URL);
return YES;
}
Is there something wrong?
回答1:
Set the following in IB > Utilities > Attributes Inspector. Notably the UITextView cannot be editable and have links enabled.
You can also do the same with code:
_textView.editable = NO;
_textView.dataDetectorTypes = UIDataDetectorTypeLink;
来源:https://stackoverflow.com/questions/21323338/ios7-uitextview-set-nslinkattributename-attribute-but-can-not-click