Placeholder in UITextView

前端 未结 30 3184
野趣味
野趣味 2020-11-22 16:01

My application uses an UITextView. Now I want the UITextView to have a placeholder similar to the one you can set for an UITextField.<

30条回答
  •  时光说笑
    2020-11-22 16:12

    I read through all of these, but came up with a very short, Swift 3, solution that has worked in all of my tests. It could stand a little more generality, but the process is simple. Here's the entire thing which I call "TextViewWithPlaceholder".

    import UIKit
    
    class TextViewWithPlaceholder: UITextView {
    
        public var placeholder: String?
        public var placeholderColor = UIColor.lightGray
    
        private var placeholderLabel: UILabel?
    
        // Set up notification listener when created from a XIB or storyboard.
        // You can also set up init() functions if you plan on creating
        // these programmatically.
        override func awakeFromNib() {
            super.awakeFromNib()
    
            NotificationCenter.default.addObserver(self,
                                               selector: #selector(TextViewWithPlaceholder.textDidChangeHandler(notification:)),
                                               name: .UITextViewTextDidChange,
                                               object: self)
    
            placeholderLabel = UILabel()
            placeholderLabel?.alpha = 0.85
            placeholderLabel?.textColor = placeholderColor
        }
    
        // By using layoutSubviews, you can size and position the placeholder
        // more accurately. I chose to hard-code the size of the placeholder
        // but you can combine this with other techniques shown in previous replies.
        override func layoutSubviews() {
            super.layoutSubviews()
    
            placeholderLabel?.textColor = placeholderColor
            placeholderLabel?.text = placeholder
    
            placeholderLabel?.frame = CGRect(x: 6, y: 4, width: self.bounds.size.width-16, height: 24)
    
            if text.isEmpty {
                addSubview(placeholderLabel!)
                bringSubview(toFront: placeholderLabel!)
            } else {
                placeholderLabel?.removeFromSuperview()
            }
        }
    
        // Whenever the text changes, just trigger a new layout pass.
        func textDidChangeHandler(notification: Notification) {
            layoutSubviews()
        }
    }
    

提交回复
热议问题