TextView
placeholder
swift3

TextViewのPlaceHolderを設定する

More than 1 year has passed since last update.

TextViewのPlaceHolderを設定する

開発環境
Swift 3
Xcode 8

実装手順

1.UITextViewを継承したClass(PlaceholderTextView.swift)を作成
2.PlaceholderTextView.swiftPlaceholderの設定を行う
3.UITextViewのカスタムクラスにPlaceholderTextView.swiftを指定

コード

ViewController.swift
import UIKit

class ViewController: UIViewController {

    // MARK: - Propaties

    @IBOutlet weak var textView: PlaceholderTextView!



    // MARK: - View Life Cycle

    override func viewDidLoad() {
        super.viewDidLoad()


        textView.placeHolder = "文字を入力してください"
        textView.placeHolderColor = UIColor(red: 0.1, green: 0.1, blue: 0.1, alpha: 0.8)

    }   //  viewDidLoad


    // MARK: - Methods

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

    }   //  didReceiveMemoryWarning



}   //  class

PlaceholderTextView.swift
import UIKit

class PlaceholderTextView: UITextView {


    // MARK: - Propaties

    lazy var placeHolderLabel: UILabel = UILabel()
    var placeHolderColor: UIColor = UIColor.lightGray
    var placeHolder: NSString = ""


    // MARK: - Initializers

    required public init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }   //  init?

    deinit {

        NotificationCenter.default.removeObserver(self)

    }   //  deinit


    // MARK: - Methods

    override public func awakeFromNib() {

        NotificationCenter.default.addObserver(self, selector: #selector(textChanged), name: NSNotification.Name.UITextViewTextDidChange, object: nil)

    }   //  awakeFromNib

    override public func draw(_ rect: CGRect) {

        if(self.placeHolder.length > 0) {

            self.placeHolderLabel.frame = CGRect(x: 8, y: 8, width: self.bounds.size.width, height: 0)
            self.placeHolderLabel.lineBreakMode = NSLineBreakMode.byWordWrapping
            self.placeHolderLabel.numberOfLines = 0
            self.placeHolderLabel.font = self.font
            self.placeHolderLabel.backgroundColor = UIColor.clear
            self.placeHolderLabel.textColor = self.placeHolderColor
            self.placeHolderLabel.alpha = 0
            self.placeHolderLabel.tag = 1

            self.placeHolderLabel.text = self.placeHolder as String
            self.placeHolderLabel.sizeToFit()
            self.addSubview(placeHolderLabel)

        }   //  if

        self.sendSubview(toBack: placeHolderLabel)

        if self.text.utf16.count == 0 && self.placeHolder.length > 0 {

            self.viewWithTag(1)?.alpha = 1

        }   //  if

        super.draw(rect)

    }   //  draw

    public func textChanged(notification: NSNotification?) -> Void {

        if self.placeHolder.length == 0 {

            return

        }   //  if

        if self.text.utf16.count == 0 {

            self.viewWithTag(1)?.alpha = 1

        }else {

            self.viewWithTag(1)?.alpha = 0

        }   //  if

    }   //  textChanged


}   //  class