Created
May 25, 2017 18:10
-
-
Save npu3pak/d1e949843a5bb5963457a04a83308aad to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
class PlaceholderTextView: UITextView { | |
@IBInspectable var placeholderColor: UIColor = UIColor.lightGray | |
@IBInspectable var placeholderText: String = "" | |
override var font: UIFont? { | |
didSet { | |
setNeedsDisplay() | |
} | |
} | |
override var contentInset: UIEdgeInsets { | |
didSet { | |
setNeedsDisplay() | |
} | |
} | |
override var textAlignment: NSTextAlignment { | |
didSet { | |
setNeedsDisplay() | |
} | |
} | |
override var text: String? { | |
didSet { | |
setNeedsDisplay() | |
} | |
} | |
override var attributedText: NSAttributedString? { | |
didSet { | |
setNeedsDisplay() | |
} | |
} | |
required init?(coder aDecoder: NSCoder) { | |
super.init(coder: aDecoder) | |
setUp() | |
} | |
override init(frame: CGRect, textContainer: NSTextContainer?) { | |
super.init(frame: frame, textContainer: textContainer) | |
} | |
private func setUp() { | |
NotificationCenter.default.addObserver(self, selector: #selector(PlaceholderTextView.textChanged(_:)), | |
name: NSNotification.Name.UITextViewTextDidChange, object: self) | |
} | |
func textChanged(_ notification: Notification) { | |
setNeedsDisplay() | |
} | |
func placeholderRectForBounds(_ bounds: CGRect) -> CGRect { | |
var x = contentInset.left + 4.0 | |
var y = contentInset.top + 9.0 | |
let w = frame.size.width - contentInset.left - contentInset.right - 16.0 | |
let h = frame.size.height - contentInset.top - contentInset.bottom - 16.0 | |
if let style = self.typingAttributes[NSParagraphStyleAttributeName] as? NSParagraphStyle { | |
x += style.headIndent | |
y += style.firstLineHeadIndent | |
} | |
return CGRect(x: x, y: y, width: w, height: h) | |
} | |
override func draw(_ rect: CGRect) { | |
if text!.isEmpty && !placeholderText.isEmpty { | |
let paragraphStyle = NSMutableParagraphStyle() | |
paragraphStyle.alignment = textAlignment | |
let attributes: [ String: AnyObject ] = [ | |
NSFontAttributeName : font!, | |
NSForegroundColorAttributeName : placeholderColor, | |
NSParagraphStyleAttributeName : paragraphStyle] | |
placeholderText.draw(in: placeholderRectForBounds(bounds), withAttributes: attributes) | |
} | |
super.draw(rect) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment