トップ «前の日記(2022-09-09) 最新 次の日記(2022-10-10)» 編集

Cocoa練習帳

iOS/iPhone/iPad/watchOS/tvOS/MacOSX/Android プログラミング, Objective-C, Cocoa, Swiftなど

2012|01|02|03|04|05|06|07|08|09|10|11|12|
2013|01|02|03|04|05|06|07|08|09|10|11|12|
2014|01|02|03|04|05|06|07|08|09|10|11|12|
2015|01|02|03|04|05|06|07|08|09|10|11|12|
2016|01|02|03|04|05|06|07|08|09|10|11|12|
2017|01|02|03|04|05|06|07|08|09|10|11|12|
2018|01|02|03|04|05|06|07|08|09|10|11|12|
2019|01|02|03|04|05|06|07|08|09|10|11|12|
2020|01|02|03|04|05|06|07|08|09|10|11|12|
2021|01|02|03|04|05|06|07|08|09|10|11|12|
2022|01|02|03|04|05|06|07|08|09|10|11|12|
2023|01|02|03|04|05|06|07|08|09|10|11|12|
2024|01|02|03|

2022-09-12 [macOS][Cocoa][Swift]ハイパー・リンクを埋め込む

Cocoaのテキストを扱うUI部品には高度な機能が用意されているということで、今回はハイパー・リンクの埋め込みを試してみた。

拡張(extension)を使ってNSAttributedStringにハイパー・リンクの埋め込み機能を追加する。

import Cocoa
 
extension NSAttributedString {
    class func hyperlink(inString: String, aURL: NSURL) -> NSAttributedString {
        let attrString = NSMutableAttributedString(string: inString)
        let range = NSMakeRange(0, attrString.length)
        
        attrString.beginEditing()
        attrString.addAttribute(.link, value: aURL.absoluteString ?? "", range: range)
        
        /* make the text appear in blue */
        attrString.addAttribute(.foregroundColor, value: NSColor.blue, range:range)
        
        /* next make the text appear with an underline */
        attrString.addAttribute(.underlineStyle, value:NSNumber(value: NSUnderlineStyle.single.rawValue), range:range)
        
        attrString.endEditing()
        
        return attrString
    }
}

NSTextFieldにハイパー・リンクが埋め込まれた文字列を設定する。

    private func setHyperlink(inTextField: NSTextField) {
        // both are needed, otherwise hyperlink won't accept mousedown
        inTextField.allowsEditingTextAttributes = true
        inTextField.isSelectable = true
        
        if let url = NSURL(string: "http://www.bitz.co.jp/") {
            let string = NSMutableAttributedString()
            string.append(NSAttributedString.hyperlink(inString: "Bitz Co., Ltd.", aURL: url))
            
            // set the attributed string to the NSTextField
            inTextField.attributedStringValue = string
        }
    }

NSTextViewにハイパー・リンクが埋め込まれた文字列を設定する。

    private func setHyperlink(inTextView: NSTextView) {
        // create the attributed string
        let string = NSMutableAttributedString()
        
            // create the url and use it for our attributed string
        if let url = NSURL(string: "http://www.bitz.co.jp/") {
            string.append(NSAttributedString.hyperlink(inString: "Bitz Co., Ltd.", aURL: url))
            
            // apply it to the NSTextView's text storage
            inTextView.textStorage?.setAttributedString(string)
        }
    }

NSTextFieldとNSTextViewの内部の構成の違いから、NSTextViewはNSTextStorageに対してハイパー・リンク付き文字列を設定する。

_ ソースコード

GitHubからどうぞ。
https://github.com/murakami/workbook/tree/master/mac/EmbeddingHyperlinks - GitHub

トップ «前の日記(2022-09-09) 最新 次の日記(2022-10-10)» 編集