swift 自定義適配系統(tǒng)UItextview

通過(guò)系統(tǒng)自帶的方法實(shí)現(xiàn)UItextview內(nèi)邊距、placehodler和開(kāi)始編輯后的不同顏色、最多顯示三行、行高。

看看效果
image.png

image.png
  
    lazy var textView: UITextView = {
        let textView = UITextView()
        textView.text = "填寫(xiě)后會(huì)被系統(tǒng)推薦"
        textView.font = UIFont.systemFont(ofSize: 14)
        textView.textColor = UIColor("#AEAEB2")
        textView.textContainerInset = UIEdgeInsets(top: 16, left: 16, bottom: 16, right: 16)
        textView.textContainer.maximumNumberOfLines = 3
        textView.textContainer.lineBreakMode = .byTruncatingTail
        textView.delegate = self
        textView.clipsToBounds = true
        textView.layer.cornerRadius = 8
        textView.bounces = false
        textView.isScrollEnabled = false
        textView.backgroundColor = ColorBackgroundColor6
        return textView
    }()
 

//MARK: -UITextViewDelegate
extension UserEditCell: UITextViewDelegate{
    func textViewDidBeginEditing(_ textView: UITextView) {
        textView.text = ""
    }
    
    func textViewDidChange(_ textView: UITextView) {
        
        textView.textColor = UIColor("#636366")
        //設(shè)置行間距
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.lineSpacing = 4
//        let content = "今天又在想,我們做很多事情,學(xué)習(xí)新的東西、認(rèn)真工作、健身,都是為了讓自己更喜歡自己一點(diǎn)。我喜歡我的時(shí)候,誰(shuí)不喜歡我都沒(méi)關(guān)系,開(kāi)開(kāi)心心的過(guò)好每一天。"
        if let content = textView.text{
            let attributedText = NSMutableAttributedString(string: content)
            attributedText.addAttribute(NSAttributedString.Key.paragraphStyle, value: paragraphStyle, range: NSRange(location: 0, length: content.count))
            textView.attributedText = attributedText
        }
      
    }
    
}

?著作權(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)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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