48
45

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Swift]でLabelの一部をリンク化させる方法

Last updated at Posted at 2016-10-29

SwiftでUILabelの先頭の文字をリンク化させて見ました

UILabelで一部リンク化させたいなと思ったのですが、ライブラリを使うのは嫌なのでちょっと調べて見ました。

環境
Xcode8
Swift3

実装

.swift
class ViewController: UIViewController {

    @IBOutlet weak var helloLabel: UILabel!
    
    let linkText = "Hello"
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        helloLabel.isUserInteractionEnabled = true
        let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(tapGesture))
        helloLabel.addGestureRecognizer(tapGestureRecognizer)
        
        // リンク化させる場所を青くさせる。
        let string = helloLabel!.text!
        let range = (string as NSString).range(of: linkText)
        let attributedString = NSMutableAttributedString(string: string)
        attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.blue, range: range)
        helloLabel.attributedText = attributedString
    }
    
    func tapGesture(gestureRecognizer: UITapGestureRecognizer) {
        guard let text = helloLabel.text else { return }
        let touchPoint = gestureRecognizer.location(in: helloLabel)
        let textStorage = NSTextStorage(attributedString: NSAttributedString(string: linkText))
        let layoutManager = NSLayoutManager()
        textStorage.addLayoutManager(layoutManager)
        let textContainer = NSTextContainer(size: helloLabel.frame.size)
        layoutManager.addTextContainer(textContainer)
        textContainer.lineFragmentPadding = 0
        let toRange = (text as NSString).range(of: linkText)
        let glyphRange = layoutManager.glyphRange(forCharacterRange: toRange, actualCharacterRange: nil)
        let glyphRect = layoutManager.boundingRect(forGlyphRange: glyphRange, in: textContainer)
        if glyphRect.contains(touchPoint) {
            print("Tapped")
        }
    }
}

これだけでlinkText内の任意の文字列をリンク化させることができます。

Simulator Screen Shot 2016.10.29 18.28.03.png

こんな感じになりました。(きもいですね)

48
45
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
48
45

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?