LoginSignup
59

More than 5 years have passed since last update.

UITextViewでのPlaceHolder(プレースホルダ)をSwiftで実装する方法

Last updated at Posted at 2015-01-04

UITextField には PlaceHolder がありますが、UITextView にはありません。調べてみると、stackoberflow にまさしくのがありました。

これをSwiftで実装したらどうなるのか?ということで調べたところQiitaに

とありましました。これを見ながら実装してみました。

環境

  • Xcode 6
  • iOS 8

実装流れ

  1. UITextField を継承した新しいClass を用意
  2. UITextField に Custom Class として UIPlaceHolderTextView を設定
  3. UITextField に placeholder を設定
  4. UITextField に 文字色(UIColor)を設定

プレビュー

placeholder.gif

実際のファイル

ViewController.swift
import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var TestWebView: UIWebView!
    @IBOutlet weak var InputView: UIPlaceHolderTextView!

    override func viewDidLoad() {
        super.viewDidLoad()

        InputView.placeHolder = "本文です。"
        InputView.placeHolderColor = UIColor(red: 255, green: 0, blue: 0, alpha: 0.5)

    }

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

public class UIPlaceHolderTextView: UITextView {

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

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

    override init(frame: CGRect){
        super.init(frame: frame)
    }

    override init() {
        super.init()
    }

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

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

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

    func setText(text:NSString) {
        super.text = text
        self.textChanged(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             = 999

            self.placeHolderLabel.text = self.placeHolder
            self.placeHolderLabel.sizeToFit()
            self.addSubview(placeHolderLabel)
        }

        self.sendSubviewToBack(placeHolderLabel)

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

        super.drawRect(rect)
    }

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

        if(countElements(self.text) == 0) {
            self.viewWithTag(999)?.alpha = 1
        }else{
            self.viewWithTag(999)?.alpha = 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
59