Help us understand the problem. What is going on with this article?

UITextViewでplaceholderを実装する(Swift)

More than 3 years have passed since last update.

UITextFieldにはplaceholderがありますが、UITextViewにはありません。
今回は、UITextViewでもplaceholderを使えるように、新しいクラスを作って見ました。

実装方法

  1. UITextViewを継承した新しいClass(ファイル)を作成
  2. UITextViewにCustomClassとしてPlaceHolderTextViewを指定
  3. ソースコードでplaceholderの文字と文字色を指定

ソースコード

ViewController.swift
import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var textView: PlaceHolderTextView!

    override func viewDidLoad() {
        super.viewDidLoad()

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

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}
PlaceHolderTextView.swift
import UIKit

public class PlaceHolderTextView: UITextView {

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

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

    deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }

    override public func awakeFromNib() {
        super.awakeFromNib()

        NSNotificationCenter.defaultCenter().addObserver(self, selector: "textChanged:", name: UITextViewTextDidChangeNotification, object: nil)
    }

    override public func drawRect(rect: CGRect) {
        if(self.placeHolder.length > 0) {
            self.placeHolderLabel.frame           = CGRectMake(8,8,self.bounds.size.width - 16,0)
            self.placeHolderLabel.lineBreakMode   = NSLineBreakMode.ByWordWrapping
            self.placeHolderLabel.numberOfLines   = 0
            self.placeHolderLabel.font            = self.font
            self.placeHolderLabel.backgroundColor = UIColor.clearColor()
            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)
        }

        self.sendSubviewToBack(placeHolderLabel)

        if(self.text.utf16.count == 0 && self.placeHolder.length > 0){
            self.viewWithTag(1)?.alpha = 1
        }

        super.drawRect(rect)
    }

    public func textChanged(notification:NSNotification?) -> (Void) {
        if(self.placeHolder.length == 0){
            return
        }

        if(self.text.utf16.count == 0) {
            self.viewWithTag(1)?.alpha = 1
        }else{
            self.viewWithTag(1)?.alpha = 0
        }
    }

}

timee
日本の労働力の減少を若者の働き方改革で解決します。好きな時に好きなだけ働けるプラットフォームタイミーを作り、人々が好きなことをできる世界を実現します。
https://timee.co.jp/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away