我试图用一个UITextView创建一个hyperlink,这样当用户点击链接时,他们就会被带到safari来打开网页。我读过textview的链接检测器,但是如果文本中有一个实际的url,那么这些样本总是显示链接检测工作的。www.google.com)。我希望它是普通文本,当点击,打开一个相关的URL。(即)谷歌是文本,点击后打开一个url www.google.com)。我如何在iOS7 7/8中实现这一点?
发布于 2014-11-16 22:09:56
使用NSAttributedString
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:@"Google"
attributes:@{ NSLinkAttributeName: [NSURL URLWithString:@"http://www.google.com"] }];
self.textView.attributedText = attributedString;当然,您可以将文本的一部分设置为链接。请阅读更多关于NSAttributedString 这里的内容。
如果您想要有更多的控制,并在打开链接之前做一些事情。可以将委托设置为UITextView。
- (void)viewDidLoad {
...
self.textView.delegate = self; // self must conform to UITextViewDelegate protocol
}
...
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange {
// Do whatever you want here
NSLog(@"%@", URL); // URL is an instance of NSURL of the tapped link
return YES; // Return NO if you don't want iOS to open the link
}发布于 2017-10-24 14:05:45
Swift 3,iOS10,Xcode 9
@sikhapol的答案真的很好,如果你确切地知道你想要解析的单词,比如“单词词典”
这都是关于在UITextView中显示的字符串本身
我的解决方案是基于文本呈现如果您可以使用href标记生成UITextView呈现UITextView标记
这里是一些代码引用,您可以使用
首先,您需要将UITextView从接口生成器或表单代码配置为
注意:不要使文本视图可编辑。
接口生成器

programmatically
let htmlData = NSString(string: "go to <a href=\"http://www.google.com\">google</a> and search for it").data(using: String.Encoding.unicode.rawValue)
let attributedString = try! NSAttributedString(data: htmlData!, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil)
yourUIViewView.isSelectable = true
yourUIViewView.dataDetectorTypes = .link
yourUIViewView.attributedText = attributedString
yourUIViewView.delegate = self对于UITextViewDelegate
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
// check for the url string for performing your own custom actions here
let urlString = URL.absoluteString
// Return NO if you don't want iOS to open the link
return true
}发布于 2018-12-13 14:08:05
我编写和使用的一个漂亮的小扩展(SWIFT4.2,在iOS 12.1上进行了测试)
extension NSAttributedString {
func replace(placeholder: String, with hyperlink: String, url: String) -> NSAttributedString {
let mutableAttr = NSMutableAttributedString(attributedString: self)
let hyperlinkAttr = NSAttributedString(string: hyperlink, attributes: [NSAttributedString.Key.link: URL(string: url)!])
let placeholderRange = (self.string as NSString).range(of: placeholder)
mutableAttr.replaceCharacters(in: placeholderRange, with: hyperlinkAttr)
return mutableAttr
}
}用法:
//Set through code or through interface builder
footerText.isSelectable = true
footerText.dataDetectorTypes = .link
//Keeps the original formatting from xib or storyboard
footerText.text = "By continuing, you are indicating that you accept our @Terms@ and @Privacy@."
footerText.attributedText = footerText.attributedText?
.replace(placeholder: "@Terms@", with: "Terms and Conditions", url: AppUrl.terms)
.replace(placeholder: "@Privacy@", with: "Privacy Policy", url: AppUrl.privacy)https://stackoverflow.com/questions/26962530
复制相似问题