通過(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
}
}
}