UITextView - 長文を表示 (スクロール可能)

f:id:oynop:20150226013851p:plain

説明

UIScrollViewのサブクラス

ソースコード

UITextViewSample.swift

import UIKit
class UITextViewSample: UIView {
    override init(frame: CGRect) {
        super.init(frame: frame)
        let tv = UITextView(frame: CGRectMake(0, 0, self.frame.width-30, 100))
        self.addSubview(tv)

        // 中心へ
        tv.center = self.center

        // 文字
        tv.text = "http://oynop.hatenablog.com/\n\nMrs. Rachel Lynde lived just where the Avonlea main road dipped down into a little hollow, fringed with alders and ladies' eardrops and traversed by a brook that had its source away back in the woods of the old Cuthbert place; it was reputed to be an intricate, headlong brook in its earlier course through those woods, with dark secrets of pool and cascade; but by the time it reached Lynde's Hollow it was a quiet, well-conducted little stream, for not even a brook could run past Mrs. Rachel Lynde's door without due regard for decency and decorum; it probably was conscious that Mrs. Rachel was sitting at her window, keeping a sharp eye on everything that passed, from brooks and children up, and that if she noticed anything odd or out of place she would never rest until she had ferreted out the whys and wherefores thereof."

        // 文字色
        tv.textColor = colorPattern.darkText()

        // フォント
        tv.font = UIFont.systemFontOfSize(10.0)

        // 中心揃え
        //tv.textAlignment = NSTextAlignment.Center
        
        // リンクをクリック可能にする (PhoneNumber, Link, Address, CalenderEvent, None, All)
        tv.dataDetectorTypes = UIDataDetectorTypes.Link
        
        // 文字を編集不可能にする
        tv.editable = false
    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

ViewController.swift

import UIKit
class ViewController: UIViewController {
    override func viewDidLoad() {       
        super.viewDidLoad()
        self.view.backgroundColor = colorPattern.back()
        self.view.addSubview(UITextViewSample(frame: self.view.frame))
    }
   
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}