简体   繁体   English

如何以编程方式将文本作为 NSTextView 中的超链接? 斯威夫特 4,Xcode 9.4

[英]How to make the text as a hyperlink in NSTextView programmatically? Swift 4, Xcode 9.4

How to make the text as a hyperlink in NSTextView programmatically?如何以编程方式将文本作为 NSTextView 中的超链接?

Like this:像这样:

Just click here to register只需点击这里注册

or like this:或像这样:

Just http://example.com to register只需http://example.com即可注册

I found this solution but it works only for iOS, not for macOS我找到了这个解决方案,但它仅适用于 iOS,不适用于 macOS

Try this:尝试这个:

let attributedString = NSMutableAttributedString(string: "Just click here to register")
let range = NSRange(location: 5, length: 10)
let url = URL(string: "https://www.apple.com")!

attributedString.setAttributes([.link: url], range: range)
textView.textStorage?.setAttributedString(attributedString)

// Define how links should look like within the text view
textView.linkTextAttributes = [
    .foregroundColor: NSColor.blue,
    .underlineStyle: NSUnderlineStyle.styleSingle.rawValue
]

If you needed set Font Size for links use this ->如果您需要为链接设置字体大小,请使用此 ->

    let input = "Your string with urls"
      
      let detector = try! NSDataDetector(types: NSTextCheckingResult.CheckingType.link.rawValue)
      let matches = detector.matches(in: input, options: [], range: NSRange(location: 0, length: input.utf16.count))
      
      let attributes = [NSAttributedString.Key.font: NSFont.systemFont(ofSize: 15)]
      let attributedString = NSMutableAttributedString(string: input, attributes: attributes)
      if matches.count > 0{
           for match in matches {
                guard let range = Range(match.range, in: input) else { continue }
                let url = input[range]
                
                attributedString.setAttributes([.link: url, .font: NSFont.systemFont(ofSize: 15)], range: match.range)
           
           
           }
      }
      
      youTextView.textStorage?.setAttributedString(attributedString)

Swift 5 / macOS 12 solution to make the link clickable and have the same font any NSTextField's you display in the same window: Swift 5 / macOS 12 解决方案使链接可点击并具有您在同一窗口中显示的任何 NSTextField 的相同字体:

Define some NSTextView IBOutlet (mine is called tutorialLink ):定义一些 NSTextView IBOutlet (我的叫做tutorialLink ):

@IBOutlet weak var tutorialLink : NSTextView!

Then here's the styling code in one function:然后是一个函数中的样式代码:

fileprivate func initTutorialLink() {
    let attributedString = NSMutableAttributedString(string: "Here's a tutorial on how to set up a Digital Ocean S3 account.")
    
    // Set font for entire string
    let fontAttributes = [NSAttributedString.Key.font: NSFont.systemFont(ofSize: 13)]
    let range2 = NSRange(location: 0, length: 61)
    attributedString.setAttributes(fontAttributes, range: range2)
    
    // Set url and same font for just the link range
    let range = NSRange(location: 21, length: 40)
    let url = URL(string: tutorialUrl)!
    attributedString.setAttributes([.link: url, NSAttributedString.Key.font:NSFont.systemFont(ofSize: 13)], range: range)
    
    // Store the attributed string
    tutorialLink.textStorage?.setAttributedString(attributedString)
    
    // Mark how link text should be colored and underlined
    tutorialLink.linkTextAttributes = [
        .foregroundColor: NSColor.systemBlue,
        .underlineStyle: NSUnderlineStyle.single.rawValue
    ]
    
    // Give non-linked text same color as other labels
    tutorialLink.textColor = .labelColor
}

In IB, you obviously need to connect your NSTextView (called Scrollable Text View in the Objects Library) to the IBOutlet.在 IB 中,您显然需要将 NSTextView(在对象库中称为 Scrollable Text View)连接到 IBOutlet。 Less obviously, you need to click the box to make the NSTextView Selectable .不太明显,您需要单击该框以使 NSTextView Selectable I don't know why exactly, but if it's not Selectable then your link will not react when clicked.我不知道究竟是为什么,但如果它不是Selectable的,那么你的链接在点击时不会做出反应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM