自定義UITextView(Swift)

iOS UITextView 屬性


有這樣一種需求,在UITextView的屬性里是沒(méi)有像UITextFiled一樣的占位文字的,那么要讓UITextView擁有占位文字,需要什么思路呢?

解決方案如下:

  • 分析:如同UITextField一樣,當(dāng)沒(méi)有輸入時(shí)顯示占位文字,當(dāng)有輸入時(shí)隨即消失,當(dāng)輸入完再刪除時(shí),又立刻出現(xiàn)
  • 由上分析,可以肯定,這個(gè)占位文字我選擇一個(gè)UILable控件,因?yàn)樗軌蝻@示文字,而且便于修改占位文字屬性
  • 通過(guò)分析,用通知或者代理來(lái)監(jiān)聽(tīng)UITextView屬性的改變,這里我使用通知!

具體代碼如下:

Swift代碼

import UIKit

/// 文本視圖
public class TCComposeTextView: UITextView {
    
    /// 占位標(biāo)簽
    fileprivate lazy var placeholderLabel = UILabel()
    
    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        
        setupUI()
        setupInputAccessoryView()
    }
    
    required public init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
    
    // MARK: - 監(jiān)聽(tīng)方法
    @objc fileprivate func textChanged() {
        placeholderLabel.isHidden = self.hasText
    }
    
    @objc fileprivate func finishInput() {
        self.resignFirstResponder()
    }
}

fileprivate extension TCComposeTextView {
    
    func setupUI() {
        NotificationCenter.default.addObserver(self, selector: #selector(textChanged), name: .UITextViewTextDidChange, object: self)
        
        placeholderLabel.text = "寫下點(diǎn)什么吧..."
        placeholderLabel.font = self.font
        placeholderLabel.textColor = UIColor.lightGray
        placeholderLabel.frame.origin = CGPoint(x: 5, y: 8)
        placeholderLabel.sizeToFit()
        
        addSubview(placeholderLabel)
        
        delegate = self
    }
    
    func setupInputAccessoryView() {
        let toolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 44.0))
        let doneItem = UIBarButtonItem(title: "完成", style: .done, target: self, action: #selector(finishInput))
        let flexibleItem = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
        toolbar.items = [flexibleItem, doneItem]
        
        self.inputAccessoryView = toolbar
    }
}

// MARK: - UITextViewDelegate
extension TCComposeTextView: UITextViewDelegate {
    public func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
        if range.location > 99 {
            return false
        } else {
            return true
        }
    }
}

寫在最后

TCComposeTextView是繼承自UITextView的自定義子類,如果使用,直接創(chuàng)建復(fù)制粘貼即可。當(dāng)然有什么問(wèn)題,可以留言給我,O(∩_∩)O謝謝!

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

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

  • iOS UITextView 屬性 有這樣一種需求,在UITextView的屬性里是沒(méi)有像UITextFiled一...
    村長(zhǎng)大人tardis_cxx閱讀 1,128評(píng)論 0 0
  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,893評(píng)論 25 709
  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,534評(píng)論 19 139
  • 阮青看著這個(gè)熟睡的男人,再看看手中的菜刀,她想就這么直接的砍下去。 這個(gè)男人叫霍齊,滿臉的油光配合著鼾聲,就像叫囂...
    sean_xw閱讀 324評(píng)論 0 2
  • 年后上班的第一個(gè)早上,我,遲到了…… 年后的柳州東路站,乘客爆增,傷不起,今天第四班才擠上去,還因?yàn)槭莻€(gè)空車≥﹏≤...
    千葉妖精閱讀 206評(píng)論 3 1

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