iOS7 UITextView set NSLinkAttributeName attribute but can not click

跟風遠走 提交于 2020-01-01 06:46:30

问题


@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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!