最近这段时间在搞一个Swift的项目,算是帮朋友做的吧,虽然有点累但是自己也是从中学到了很多东西,其中自己也封装了一些常用的控件,最近忙完公司的项目以后就整理一下自己的心得。。希望大家一起学习!
最近项目需要,很多的地方都用到了UITextView来实现一些需求,需要设置占位词和最大的长度,这里我是简单的封装了一下,希望大家多多指正,话不多说,上代码:
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
if text == "\n"{ // 输入换行符时收起键盘
textView.resignFirstResponder() // 收起键盘
return false
}
if text != "" {
self.placeHolderLabel.isHidden = true // 隐藏
}
let textStr = NSString.init(format: "%@", text)
if textStr.isEqual(to: "") && range.length == 1 && range.location == 0 {
self.placeHolderLabel.isHidden = false // 隐藏
}
/// 字数统计
let lengthOfString :NSInteger = textStr.length
let fieldTextString : String = textV.text
let proposedNewLength : Int = fieldTextString.count - range.length + lengthOfString;
maxLengthNum.text = NSString.init(format: "%ld/%ld", proposedNewLength, maxNumber) as String
if proposedNewLength >= maxNumber + 1 {
Logger.d("已到字数限制")
let textVStr = textV.text as NSString
let str = textVStr.substring(to: maxNumber)
textV.text = str
maxLengthNum.text = NSString.init(format: "%ld/%ld", maxNumber, maxNumber) as String
return false
}
return true
}
在 UITextView的代理方法中监听内容的变化,来判断占位词的显示和隐藏,设置当前的字数以及最大长度,这里对字数的统计比较重要,我是根据当前输入的内容的位置来进行计算。
全部代码:
import UIKit
class LZHTextView: UIView, UITextViewDelegate {
/** 占位词 **/
let placeHolderLabel = UILabel()
/** 字数限制 **/
let maxLengthNum = UILabel()
/** 最大字数 **/
var maxNumber: NSInteger = 0
/** 文本View */
let textV = UITextView()
override init(frame: CGRect) {
super.init(frame: frame)
textV.backgroundColor = .white
textV.returnKeyType = .done
textV.font = APP_FONT_14_NORMAL()
textV.textColor = .black
textV.delegate = self
self.addSubview(textV)
textV.snp.makeConstraints { (make) in
make.top.left.right.equalToSuperview()
make.bottom.equalToSuperview().offset(-20)
}
placeHolderLabel.textColor = RGBColor(135, g: 135, b: 135)
placeHolderLabel.font = APP_FONT_12_SAMALL()
placeHolderLabel.isHidden = false
textV.addSubview(placeHolderLabel)
placeHolderLabel.snp.makeConstraints { (make) in
make.left.equalToSuperview().offset(5)
make.right.right.equalToSuperview()
make.top.equalToSuperview()
make.height.equalTo(25)
}
/// 字数限制
maxLengthNum.textColor = APP_Main_GrayText_COLOR
maxLengthNum.font = APP_FONT_12_SAMALL()
maxLengthNum.textAlignment = .center
self.addSubview(maxLengthNum)
maxLengthNum.snp.makeConstraints { (make) in
make.bottom.equalToSuperview()
make.right.equalToSuperview().offset(-10)
make.width.equalTo(50)
make.height.equalTo(20)
}
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
/**
* 设置占位词
*/
func setPlaceHolder(holderStr: String) -> Void {
placeHolderLabel.text = holderStr
}
/**
* 设置字数限制
*/
func setContentMaxLength(length: NSInteger) -> Void {
if length == 0 {
maxLengthNum.isHidden = true
} else {
maxLengthNum.isHidden = false
maxLengthNum.text = NSString.init(format: "0/%ld", length) as String
}
maxNumber = length
}
func textViewShouldBeginEditing(_ textView: UITextView) -> Bool {
self.placeHolderLabel.isHidden = true // 隐藏
return true
}
func textViewDidEndEditing(_ textView: UITextView) {
if textView.text.isEmpty {
self.placeHolderLabel.isHidden = false // 显示
}
}
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
if text == "\n"{ // 输入换行符时收起键盘
textView.resignFirstResponder() // 收起键盘
return false
}
if text != "" {
self.placeHolderLabel.isHidden = true // 隐藏
}
let textStr = NSString.init(format: "%@", text)
if textStr.isEqual(to: "") && range.length == 1 && range.location == 0 {
self.placeHolderLabel.isHidden = false // 隐藏
}
/// 字数统计
let lengthOfString :NSInteger = textStr.length
let fieldTextString : String = textV.text
let proposedNewLength : Int = fieldTextString.count - range.length + lengthOfString;
maxLengthNum.text = NSString.init(format: "%ld/%ld", proposedNewLength, maxNumber) as String
if proposedNewLength >= maxNumber + 1 {
Logger.d("已到字数限制")
let textVStr = textV.text as NSString
let str = textVStr.substring(to: maxNumber)
textV.text = str
maxLengthNum.text = NSString.init(format: "%ld/%ld", maxNumber, maxNumber) as String
return false
}
return true
}
}
里面修改一些字体大小的设置以及颜色,引入SnapKit框架就可以直接用,有不足的地方还望大佬们指正。