Swift.為TextView進(jìn)行添加PlaceHolder屬性

效果圖

實(shí)現(xiàn)效果:

為TextView添加placeHolder屬性,可以直接賦值使用,并且可以通過placeHolderColor方法對其顏色進(jìn)行修改。

實(shí)現(xiàn)思路:

新建類繼承自UITextView,添加placeHolder,placeHolderColor等屬性,重寫drawRect方法,將placeHolder畫入textView,通過通知和didset進(jìn)行drawRect的方法調(diào)用。

完整代碼

class EWTextView: UITextView {
    /// setNeedsDisplay調(diào)用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))
    }
}

調(diào)用方法:

新建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

有問題歡迎探討.

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容