美文网首页
UITextView的封装(限定字符个数)

UITextView的封装(限定字符个数)

作者: Eyes_cc | 来源:发表于2019-11-22 13:58 被阅读0次
关键代码:

String的知识点:

// 取前50个字符
let index = textView.text.index(textView.text.startIndex, offsetBy: 50)
textView.text = String(textView.text.prefix(upTo: index))

示例图

示例图
class CNTextView: UIView, UITextViewDelegate {
    // MARK: Properties
    // 意见说明
    var textView: UITextView =  {
        let textView = UITextView()
        textView.font               = UIFont.adjustFontSize(ofSize: 15)
        textView.returnKeyType = .done
        textView.backgroundColor = UIColor.init(red: 248/255, green: 248/255, blue: 248/255, alpha: 1)
        return textView
    }()
    let place: UILabel = {
        let label = UILabel()
        label.text = "请输入"
        label.textColor = UIColor.init(red: 153/255, green: 153/255, blue: 153/255, alpha: 1)
        label.font = UIFont.adjustFontSize(ofSize: 14)
        return label
    }()
    let numberRemind: UILabel = {
        let label = UILabel()
        label.text = "0/50"
        label.textColor = UIColor.init(red: 153/255, green: 153/255, blue: 153/255, alpha: 1)
        label.font = UIFont.adjustFontSize(ofSize: 14)
        return label
    }()
    // MARK: LifeCycle
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.white
        // AddView
        self.addSubview(textView)
        self.addSubview(place)
        self.addSubview(numberRemind)
        textView.delegate  = self
        self.setUI()
    }
    // Events
    func setUI() {
        textView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(15)
        }
        place.snp.makeConstraints { (make) in
            make.left.equalTo(textView).offset(10)
            make.top.equalTo(textView).offset(8)
        }
        numberRemind.snp.makeConstraints { (make) in
            make.right.bottom.equalTo(textView)
        }
    }
    
    // UITextViewDelegate
    func textViewDidChange(_ textView: UITextView) {
        if textView.text == "" {
            place.isHidden = false
        } else {
            place.isHidden = true
            if textViewFendian.text.count < 50 {
                numberRemind.text = "\(textViewFendian.text.count)" + "/50"
            } else {
                numberRemind.text = "50/50"
                let index = textView.text.index(textView.text.startIndex, offsetBy: 50)
                textView.text = String(textView.text.prefix(upTo: index))
            }
            
        }
    }
    func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
        if text == "\n" {
            textView.resignFirstResponder()
            return false
        }
        
        return true
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

相关文章

网友评论

      本文标题:UITextView的封装(限定字符个数)

      本文链接:https://www.haomeiwen.com/subject/rwvfwctx.html