LoginSignup
3
3

More than 5 years have passed since last update.

文字(UITextView)の一部をHyperlink化させる

Last updated at Posted at 2016-10-05

見返すためにちょこっとメモ
The code in this post is written in Swift 2.3. Sorry for Swift 3.0 users lol
I will convert this to Swift 3.0 soon.

Development Environment

  • OS X El Captain 10.11.2
  • Xcode Version 8.0

Language

Swift 2.3

Code

ViewController.swift
import UIKit
class ViewController: UIViewController {
    @IBOutlet weak var textView: UITextView!
    var textRange: NSRange = NSRange()

    override func viewDidLoad() {
        super.viewDidLoad()
        setStrings()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}
extension ViewController{
    func setStrings(){
        // for Text
        textView.text = "Qiitaに投稿!自分用のメモとして使用しています!"
        textView.userInteractionEnabled = true
        let text = textView.text!
        textView.backgroundColor = UIColor.clearColor()

        let linkText = "Qiita"
        textView.editable = false
        textView.selectable = true
        textView.scrollEnabled = false
        textView.textAlignment = .Center

        let nsTex = text as NSString
        let linkRange = text.rangeOfString(linkText)

        let style = NSMutableParagraphStyle()
        style.alignment = NSTextAlignment.Center

        let attributedString = NSMutableAttributedString(string: text, attributes: [ NSParagraphStyleAttributeName: style ])

        let start = text.startIndex.distanceTo(linkRange!.startIndex)
        let length = linkRange!.startIndex.distanceTo(linkRange!.endIndex)
        textRange = NSMakeRange(start, length)

        /*リンク位置範囲生成*/
        // all text colour
        attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.whiteColor(), range: NSMakeRange(0, nsTex.length))
        // link colour
        attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.whiteColor(), range: textRange)

        // underline
        attributedString.addAttribute(NSUnderlineStyleAttributeName, value: NSUnderlineStyle.StyleSingle.rawValue, range: textRange)
        textView.attributedText = attributedString
        let tapGes = UITapGestureRecognizer(target: self, action: #selector(ViewController.tapText(_:)))    
        textView.addGestureRecognizer(tapGes)

    func tapText(tap: UITapGestureRecognizer) {
        let location = tap.locationInView(textView)
        let textPosition = textView.closestPositionToPoint(location)
        let selectedPosition = textView.offsetFromPosition(textView.beginningOfDocument, toPosition: textPosition!)
        if NSLocationInRange(selectedPosition, textRange) {
            let url = NSURL(string: "http://qiita.com/")
            if UIApplication.sharedApplication().canOpenURL(url!){
                UIApplication.sharedApplication().openURL(url!)
            }
        }
    }
}
3
3
0

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
3
3