Swift.为TextView进行添加PlaceHolder属性

效果图

实现效果:

为TextView添加placeHolder属性,可以直接赋值使用,并且可以通过placeHolderColor方法对其颜色进行修改。

实现思路:

新建类继承自UITextView,添加placeHolder,placeHolderColor等属性,重写drawRect方法,将placeHolder画入textView,通过通知和didset进行drawRect的方法调用。

完整代码

class EWTextView: UITextView {
    /// setNeedsDisplay调用drawRect
    var placeHolder: String = ""{
        didSet{
            self.setNeedsDisplay()
        }
    }
    var placeHolderColor: UIColor = UIColor.gray{
        didSet{
            self.setNeedsDisplay()
        }
    }
    override var font: UIFont?{
        didSet{
            self.setNeedsDisplay()
        }
    }
    override var text: String!{
        didSet{
            self.setNeedsDisplay()
        }
    }
    override var attributedText: NSAttributedString!{
        didSet{
            self.setNeedsDisplay()
        }
    }
    
    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        /// default字号
        self.font = UIFont.systemFont(ofSize: 14)
        NotificationCenter.default.addObserver(self, selector: #selector(textDidChanged(noti:)), name: UITextView.textDidChangeNotification, object: self)
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    @objc func textDidChanged(noti: NSNotification)  {
        self.setNeedsDisplay()
    }
    override func draw(_ rect: CGRect) {
        if self.hasText {
            return
        }
        var newRect = CGRect()
        newRect.origin.x = 5
        newRect.origin.y = 7
        let size = self.placeHolder.getStringSize(rectSize: rect.size, font: self.font ?? UIFont.systemFont(ofSize: 14))
        newRect.size.width = size.width
        newRect.size.height = size.height
        /// 将placeHolder画在textView上
        (self.placeHolder as NSString).draw(in: newRect, withAttributes: [NSAttributedString.Key.font: self.font ?? UIFont.systemFont(ofSize: 14),NSAttributedString.Key.foregroundColor: self.placeHolderColor])
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        self.setNeedsDisplay()
    }
    
    deinit {
        NotificationCenter.default.removeObserver(self, name: UITextView.textDidChangeNotification, object: self)
    }
    
}

extension String {
    /// 计算字符串的尺寸
    ///
    /// - Parameters:
    ///   - text: 字符串
    ///   - rectSize: 容器的尺寸
    ///   - fontSize: 字体
    /// - Returns: 尺寸
    ///
    public func getStringSize(rectSize: CGSize,font: UIFont) -> CGSize {
        let str: NSString = self as NSString
        let rect = str.boundingRect(with: rectSize, options: NSStringDrawingOptions.usesLineFragmentOrigin, attributes: [NSAttributedString.Key.font: font], context: nil)
        return CGSize(width: ceil(rect.width), height: ceil(rect.height))
    }
}

调用方法:

新建textView,直接为placeHolder和placeHolderColor属性赋值。

        let textView = EWTextView(frame: CGRect(x: 50, y: 100, width: 250, height: 400))
        textView.backgroundColor = UIColor.brown
        textView.placeHolder = "这是placeHolder"
        textView.placeHolderColor = UIColor.green
        textView.font = UIFont.systemFont(ofSize: 20)
        self.view.addSubview(textView)

demo地址:EWTextView

有问题欢迎探讨.

你可能感兴趣的:(Swift.为TextView进行添加PlaceHolder属性)