Skip to content

Instantly share code, notes, and snippets.

@albertbori
Last active January 13, 2020 09:25
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save albertbori/bcf21c680e093a6dfc42 to your computer and use it in GitHub Desktop.
Save albertbori/bcf21c680e093a6dfc42 to your computer and use it in GitHub Desktop.
UITextView support for Placeholder text
import UIKit
import Foundation
@IBDesignable class PlaceholderTextView: UITextView, UITextViewDelegate
{
private let _placeholderColor: UIColor = UIColor(white: 0.78, alpha: 1)
private var _placeholderLabel: UILabel!
@IBInspectable var placeholder: String = "" {
didSet {
_placeholderLabel.text = placeholder
}
}
override var text: String! {
didSet {
textViewDidChange(self)
}
}
override init() {
super.init()
configurePlaceholderLabel()
}
override init(frame: CGRect) {
super.init(frame: frame)
}
override init(frame: CGRect, textContainer: NSTextContainer?) {
super.init(frame: frame, textContainer: textContainer)
configurePlaceholderLabel()
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
configurePlaceholderLabel()
}
func configurePlaceholderLabel() {
self.delegate = self
_placeholderLabel = UILabel()
_placeholderLabel.font = font
_placeholderLabel.textColor = _placeholderColor
_placeholderLabel.text = placeholder
_placeholderLabel.numberOfLines = 0
_placeholderLabel.setTranslatesAutoresizingMaskIntoConstraints(false)
self.addSubview(_placeholderLabel)
var constraints = NSLayoutConstraint.constraintsWithVisualFormat("H:|-(\(self.frame.origin.x))-[placeholder]-(\(0))-|", options: nil, metrics: nil, views: ["placeholder": _placeholderLabel])
constraints += NSLayoutConstraint.constraintsWithVisualFormat("V:|-(\(self.textContainerInset.top))-[placeholder]-(>=\(self.textContainerInset.bottom))-|", options: nil, metrics: nil, views: ["placeholder": _placeholderLabel])
self.addConstraints(constraints)
}
func textViewDidChange(textView: UITextView) {
_placeholderLabel.hidden = !textView.text.isEmpty
}
}

Usage

Interface builder

  1. Add a UITextView control onto the nib or storyboard UIViewController
  2. In the Identity Inspectorpane , select the class PlaceholderTextView
  3. In the Attributes Inspector pane, there should be a "Placeholder" text field, which will let you set the placeholder text.

Note: There is an existing bug in interface builder which will cause this control to crash the preview engine. If this bothers you, remove @IBDesignable and @IBInspectable attributes, then set the placeholder property from code

Code Example

var textView = PlaceholderTextView(frame: CGRect(x: 0, y: 0, width: 300, height: 44))
textView.placeholder = "Username"
self.view.addSubview(textView)
@danipralea
Copy link

This is awesome. Thanks for sharing!

@johnnye
Copy link

johnnye commented May 11, 2016

I would also add
self.sendSubviewToBack(_placeholderLabel)
This means that the carat doesn't appear below the placeholder when you first tap on the textview.

@RogelioHIT
Copy link

I will use this, thanks for sharing 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment