iOS:是否可以将超链接文本更改为不同的名称,但仍会将您重定向到同一站点?

iOS: is it possible to change the hyperlink text to a different name yet still redirects you to the same site?

例如,下面的代码创建了一个指向 google.com 的超链接。但是,我需要文本说 "click here" 并且仍然转到 google.com。我必须以文本形式(没有按钮或任何其他对象)。

编辑:myView 是一个文本视图。抱歉,忘记添加了。

myView.text = @"http://google.com";
myView.editable = NO;

是的,这是可能的。您需要使用属性字符串

NSMutableAttributedString * str = [[NSMutableAttributedString alloc] initWithString:@"click here"];
[str addAttribute: NSLinkAttributeName value: @"http://www.google.com" range: NSMakeRange(0, str.length)];
myView.attributedText = str;

还要确保您符合 UITextViewDelegate 并实现此方法

- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)url inRange:(NSRange)characterRange
{
     return YES;
}

一个警告 - NSLinkAttributeName 看起来更喜欢 NSURL,但文档说它也应该使用字符串。