簡體   English   中英

如何以編程方式將文本作為 NSTextView 中的超鏈接? 斯威夫特 4,Xcode 9.4

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

如何以編程方式將文本作為 NSTextView 中的超鏈接?

像這樣:

只需點擊這里注冊

或像這樣:

只需http://example.com即可注冊

我找到了這個解決方案,但它僅適用於 iOS,不適用於 macOS

嘗試這個:

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
]

如果您需要為鏈接設置字體大小,請使用此 ->

    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 解決方案使鏈接可點擊並具有您在同一窗口中顯示的任何 NSTextField 的相同字體:

定義一些 NSTextView IBOutlet (我的叫做tutorialLink ):

@IBOutlet weak var tutorialLink : NSTextView!

然后是一個函數中的樣式代碼:

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
}

在 IB 中,您顯然需要將 NSTextView(在對象庫中稱為 Scrollable Text View)連接到 IBOutlet。 不太明顯,您需要單擊該框以使 NSTextView Selectable 我不知道究竟是為什么,但如果它不是Selectable的,那么你的鏈接在點擊時不會做出反應。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM