JKSwiftExtension 中UITextView+Extension
和 UITextField+Extension
有源码
- 目录:
- 1、背景介绍
支持表情等长度问题 - 2、使用
- 1、背景介绍
一、背景介绍
平时在开发中免不了限制输入的字符个数,如果涉及到(高亮和九宫格数字<➋➌➍➎➏➐➑➒>的问题)中的时候就会有各种问题,所以我们要处理这种情况
二、UITextView&UITextField 使用
-
2.1、UITextView+Extension扩展 和 UITextField+Extension扩展
public extension JKPOP where Base: UITextView { // MARK: 3.1、限制字数的输入(可配置正则)(提示在:- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;方法里面调用) /// 限制字数的输入(可配置正则) /// - Parameters: /// - range: 范围 /// - text: 输入的文字 /// - maxCharacters: 限制字数 /// - regex: 可输入内容(正则) /// - isInterceptString: 复制文字进来,在字数限制的情况下,多余的字体是否截取掉,默认true /// - Returns: 返回是否可输入 func inputRestrictions(shouldChangeTextIn range: NSRange, replacementText text: String, maxCharacters: Int, regex: String?, isInterceptString: Bool = true, lenghType: StringTypeLength = .count) -> Bool { guard !text.isEmpty else { return true } guard let oldContent = self.base.text else { return false } if let markedTextRange = self.base.markedTextRange { // 有高亮 if range.length == 0 { // 联想中 return oldContent.jk.typeLengh(lenghType) + 1 <= maxCharacters } else { // 正则的判断 if let weakRegex = regex, !JKRegexHelper.match(text, pattern: weakRegex) { return false } // 联想选中键盘 let markedRange = rangeFromTextRange(textRange: markedTextRange) // 联想选中键盘 let allContent = oldContent.jk.replacingCharacters(range: markedRange) + text if allContent.jk.typeLengh(lenghType) > maxCharacters { let newContent = allContent.jk.sub(to: maxCharacters) self.base.text = newContent return false } } } else { guard !text.jk.isNineKeyBoard() else { return true } // 正则的判断 if let weakRegex = regex, !JKRegexHelper.match(text, pattern: weakRegex) { return false } // debugPrint("没有range---------:NO 内容:\(oldContent) 长度:\(oldContent.count) 新的内容:\(text) 长度:\(text.count) range:\(range)") // 2、如果数字大于指定位数,不能输入 guard oldContent.jk.typeLengh(lenghType) + text.jk.typeLengh(lenghType) <= maxCharacters else { // 判断字符串是否要截取 guard isInterceptString else { // 不截取,也就是不让输入进去 return false } if oldContent.jk.typeLengh(lenghType) < maxCharacters { let remainingLength = maxCharacters - oldContent.jk.typeLengh(lenghType) let copyString = text.jk.removeBeginEndAllSapcefeed // debugPrint("范围:\(range) copy的字符串:\(copyString) 长度:\(copyString.count) 截取的字符串:\(copyString.jk.sub(to: remainingLength))") // 可以插入字符串 let replaceContent = copyString.jk.sub(to: remainingLength) // let newString = oldContent.jk.insertString(content: replaceContent, locat: range.location) let newString = oldContent.jk.replacingCharacters(range: range, replacingString: replaceContent) // debugPrint("老的字符串:\(oldContent) 新的的字符串:\(newString) 长度:\(newString.count)") self.base.text = newString // 异步改变 JKAsyncs.asyncDelay(0.05) {} _: { if let selectedRange = self.base.selectedTextRange { if let newPosition = self.base.position(from: selectedRange.start, offset: remainingLength) { self.base.selectedTextRange = self.base.textRange(from: newPosition, to: newPosition) } } } } return false } } return true } }
-
2.2、使用:我们以:UItextView为例,在
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;
里面调用func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool { return textView.inputRestrictions(shouldChangeTextIn: range, replacementText: text, maxCharacters: maxCharacters, regex: regex) } func textViewDidChange(_ textView: UITextView) { print("字符数量:\(textView.text.count > maxCharacters ? maxCharacters : textView.text.count)") }