首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >UITextView中的超链接

UITextView中的超链接
EN

Stack Overflow用户
提问于 2014-11-16 21:59:14
回答 6查看 32.2K关注 0票数 22

我试图用一个UITextView创建一个hyperlink,这样当用户点击链接时,他们就会被带到safari来打开网页。我读过textview的链接检测器,但是如果文本中有一个实际的url,那么这些样本总是显示链接检测工作的。www.google.com)。我希望它是普通文本,当点击,打开一个相关的URL。(即)谷歌是文本,点击后打开一个url www.google.com)。我如何在iOS7 7/8中实现这一点?

EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2014-11-16 22:09:56

使用NSAttributedString

代码语言:javascript
复制
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:@"Google" 
                                                                       attributes:@{ NSLinkAttributeName: [NSURL URLWithString:@"http://www.google.com"] }];
self.textView.attributedText = attributedString;

当然,您可以将文本的一部分设置为链接。请阅读更多关于NSAttributedString 这里的内容。

如果您想要有更多的控制,并在打开链接之前做一些事情。可以将委托设置为UITextView

代码语言:javascript
复制
- (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
}
票数 32
EN

Stack Overflow用户

发布于 2017-10-24 14:05:45

Swift 3,iOS10,Xcode 9

@sikhapol的答案真的很好,如果你确切地知道你想要解析的单词,比如“单词词典”

这都是关于在UITextView中显示的字符串本身

我的解决方案是基于文本呈现如果您可以使用href标记生成UITextView呈现UITextView标记

这里是一些代码引用,您可以使用

首先,您需要将UITextView从接口生成器或表单代码配置为

  1. 可选
  2. 数据探测

注意:不要使文本视图可编辑。

接口生成器

programmatically

代码语言:javascript
复制
         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

代码语言:javascript
复制
    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
     }
票数 18
EN

Stack Overflow用户

发布于 2018-12-13 14:08:05

我编写和使用的一个漂亮的小扩展(SWIFT4.2,在iOS 12.1上进行了测试)

代码语言:javascript
复制
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
    }
}

用法:

代码语言:javascript
复制
//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)
票数 12
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26962530

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档