UITextView with placeholder

来源:互联网 发布:阿里云邮寄幕布要钱吗 编辑:程序博客网 时间:2024/05/10 11:05
class TMTextView: UITextView {    private var placeholderLabel: UILabel!    func setPlaceholder(placeholderText: String) {        NSNotificationCenter.defaultCenter().addObserver(self, selector: "textDidChange", name: UITextViewTextDidChangeNotification, object: nil)        self.placeholderLabel = UILabel(frame: CGRectZero)        self.addSubview(self.placeholderLabel)        self.placeholderLabel.font = self.font        let size = (placeholderText as NSString).sizeWithAttributes([NSFontAttributeName: self.font])        self.placeholderLabel.frame = CGRect(x: 5, y: 5, width: size.width, height: size.height)        self.placeholderLabel.text = placeholderText    }    func textDidChange() {        self.placeholderLabel.hidden = !self.text.isEmpty    }    deinit {        NSNotificationCenter.defaultCenter().removeObserver(self)    }}
0 0