3
iOS7的UITextView設置NSLinkAttributeName屬性,但不能點擊
@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;
}
有什麼錯嗎?
吳丹的吧!非常感謝你! – jxdwinter
也是必要的:textView.selectable需要是YES,否則這將無法正常工作 – Wirsing
感謝您爲此添加gotcha @Wirsing!惱人的是,這意味着用戶也可能意外地選擇了文字... – jowie