2016-07-12 8 views

答えて

2

最も簡単な方法は、HyperlinkTextFieldを作成するためにNSTextFieldをサブクラス化することでいただければ幸いです。以下は例です:

まずは、プロジェクトにHyperlinkTextFieldクラスを追加してみましょう。

// HyperlinkTextField.swift 

import Cocoa 

@IBDesignable 
class HyperlinkTextField: NSTextField { 
    @IBInspectable var href: String = "" 

    override func awakeFromNib() { 
     super.awakeFromNib() 

     let attributes: [String: AnyObject] = [ 
      NSForegroundColorAttributeName: NSColor.blueColor(), 
      NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleSingle.rawValue 
     ] 
     self.attributedStringValue = NSAttributedString(string: self.stringValue, attributes: attributes) 
    } 

    override func mouseDown(theEvent: NSEvent) { 
     NSWorkspace.sharedWorkspace().openURL(NSURL(string: self.href)!) 
    } 
} 

次に、Interface Builderで、あなたの窓に、オブジェクトライブラリからラベルをドラッグします。

インスペクタ(Cmd + Opt + 4属性メニュー表示>ユーティリティ>ショーアイデンティティインスペクタに行く(またはCmd + Opt + 3を押して)、そのラベルを選択し、HyperlinkTextField

Identity Inspector

行くようにクラスを変更)訪問したいURLにHrefを設定してください。

Attributes Inspector

ラベルは、Interface Builderで黒のテキストを示していますが、あなたのアプリケーションを実行するとすべてがうまくなります。ラベルをクリックすると、デフォルトブラウザのリンクが開きます。

私が達成できなかったことの1つは、HyperlinkTextFieldを青色で表示し、Interface Builderで下線を付けることでした。それを行う方法に関するコメントは大歓迎です。

+0

これは素晴らしいです、ありがとうございます。 – jeremyforan

2

正確に私が必要としたもの。あなたのような何かを行うことができますので、下線付きと青になるために、ラベルのテキストの部分文字列を可能にするために、既存の回答を変更

import Cocoa 

@IBDesignable 
class HyperTextField: NSTextField { 
    @IBInspectable var href: String = "" 

    override func awakeFromNib() { 
     super.awakeFromNib() 

     let attributes: [String: AnyObject] = [ 
      NSForegroundColorAttributeName: NSColor.blue, 
      NSUnderlineStyleAttributeName: NSUnderlineStyle.styleSingle.rawValue as AnyObject 
     ] 
     self.attributedStringValue = NSAttributedString(string: self.stringValue, attributes: attributes) 
    } 

    override func mouseDown(with event: NSEvent) { 
     NSWorkspace.shared().open(URL(string: self.href)!) 
    } 
} 
0

Thisは答えは

// A text field that can contain a hyperlink within a range of characters in the text. 
@IBDesignable 
public class SubstringLinkedTextField: NSTextField { 
    // the URL that will be opened when the link is clicked. 
    public var link: String = "" 
    @available(*, unavailable, message: "This property is reserved for Interface Builder. Use 'link' instead.") 
    @IBInspectable public var HREF: String { 
     get { 
      return self.link 
     } 
     set { 
      self.link = newValue 
      self.needsDisplay = true 
     } 
    } 

    // the substring within the field's text that will become an underlined link. if empty or no match found, the entire text will become the link. 
    public var linkText: String = "" 
    @available(*, unavailable, message: "This property is reserved for Interface Builder. Use 'linkText' instead.") 
    @IBInspectable public var LinkText: String { 
     get { 
      return self.linkText 
     } 
     set { 
      self.linkText = newValue 
      self.needsDisplay = true 
     } 
    } 

    override public func awakeFromNib() { 
     super.awakeFromNib() 

     self.allowsEditingTextAttributes = true 
     self.isSelectable = true 

     let url = URL(string: self.link) 
     let attributes: [NSAttributedStringKey: AnyObject] = [ 
      NSAttributedStringKey(rawValue: NSAttributedStringKey.link.rawValue): url as AnyObject 
     ] 
     let attributedStr = NSMutableAttributedString(string: self.stringValue) 

     if self.linkText.count > 0 { 
      if let range = self.stringValue.indexOf(substring: self.linkText) { 
       attributedStr.setAttributes(attributes, range: range) 
      } else { 
       attributedStr.setAttributes(attributes, range: NSMakeRange(0, self.stringValue.count)) 
      } 
     } else { 
      attributedStr.setAttributes(attributes, range: NSMakeRange(0, self.stringValue.count)) 
     } 
     self.attributedStringValue = attributedStr 
    } 
} 
1

です。ここSwift3バージョンですスイフト4、Xcodeの9

@IBDesignable 
class HyperlinkTextField: NSTextField { 

@IBInspectable var href: String = "" 

override func resetCursorRects() { 
    discardCursorRects() 
    addCursorRect(self.bounds, cursor: NSCursor.pointingHand) 
} 

override func awakeFromNib() { 
    super.awakeFromNib() 

    // TODO: Fix this and get the hover click to work. 

    let attributes: [NSAttributedStringKey: Any] = [ 
     NSAttributedStringKey.foregroundColor: NSColor.blue, 
     NSAttributedStringKey.underlineStyle: NSUnderlineStyle.styleSingle.rawValue as AnyObject 
    ] 
    attributedStringValue = NSAttributedString(string: self.stringValue, attributes: attributes) 
} 

override func mouseDown(with theEvent: NSEvent) { 
    if let localHref = URL(string: href) { 
     NSWorkspace.shared.open(localHref) 
    } 
} 

}

0
let link = NSTextField() 
link.isBezeled = false 
link.drawsBackground = false 
link.isEditable = false 
link.isSelectable = true 
link.allowsEditingTextAttributes = true 
let url = URL(string: "http://www.google.com") 
let linkTextAttributes: [NSAttributedStringKey: Any] = [ 
     NSAttributedStringKey.underlineStyle: NSUnderlineStyle.styleSingle.rawValue, 
     NSAttributedStringKey.foregroundColor: NSColor.blue, 
     NSAttributedStringKey.link: url as Any 
      ] 
let string = "whatever" 
link.attributedStringValue = NSAttributedString(string: string, attributes: linkTextAttributes) 
window.contentView?.addSubview(link) 
関連する問題