前言
平时输入会有输入限制,
1.最大字符数量
2.限制特殊字符:表情包、数字中文、大小写
3.空格换行符等特殊字符
一般的解决办法在输入时或输入完成时(根据业务需要),设置正则过滤就可以。
参考:常用正则表达式(通用字符过滤)
注意,今天说iOS的输入限制,很离谱,以下为一些坑点:
1.明确是需要显示还是直接不允许输入(限制输入打不出来,可以打出来但超过后提示);
2.边输入边查询还是输入完成后再查询。(这些都是需求定义类的,注意提前划分);
3.用正则的时候注意连续字符输入;
4.限制表情输入注意九宫格输入时是表情➋➌➍➎➏➐➑➒,所以还得兼容一下九宫格;(很让人费解)
…
正文
0.工具类
MyTextViewAndTextFieldManager
.h
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface MyTextViewAndTextFieldManager : NSObject
/**
* 判断字符串中是否存在emoji
* @param string 字符串
* @return YES(含有表情)
*/
+ (BOOL)stringContainsEmoji:(NSString *)string;
//1、首先上判断字符串是否含有表情符号的代码
/**
* 判断字符串中是否存在emoji
* @param string 字符串
* @return YES(含有表情)
*/
+ (BOOL)hasEmoji:(NSString*)string;
//2、然后是去除字符串中的表情
//去除字符串中所带的表情
+ (NSString *)disable_emoji:(NSString *)text;
//3、由于苹果系统自带的九宫格输入汉字的过程中也会默认是表情符号,因此需要添加判断当前是否是九宫格键盘
/**
判断是不是九宫格
@param string 输入的字符
@return YES(是九宫格拼音键盘)
*/
+(BOOL)isNineKeyBoard:(NSString *)string;
/// textField 最长长度 WithMaxLength:20
+ (NSString *)string:(UITextField *)textField WithMaxLength:(CGFloat)maxLength;
@end
NS_ASSUME_NONNULL_END
.m
#import "MyTextViewAndTextFieldManager.h"
@implementation MyTextViewAndTextFieldManager
/**
* 判断字符串中是否存在emoji
* @param string 字符串
* @return YES(含有表情)
*/
+ (BOOL)stringContainsEmoji:(NSString *)string {
__block BOOL returnValue = NO;
[string enumerateSubstringsInRange:NSMakeRange(0, [string length]) options:NSStringEnumerationByComposedCharacterSequences usingBlock:
^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
const unichar hs = [substring characterAtIndex:0];
// surrogate pair
if (0xd800 <= hs && hs <= 0xdbff) {
if (substring.length > 1) {
const unichar ls = [substring characterAtIndex:1];
const int uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
if (0x1d000 <= uc && uc <= 0x1f77f) {
returnValue = YES;
}
}
} else if (substring.length > 1) {
const unichar ls = [substring characterAtIndex:1];
if (ls == 0x20e3) {
returnValue = YES;
}
} else {
// non surrogate
if (0x2100 <= hs && hs <= 0x27ff) {
returnValue = YES;
} else if (0x2B05 <= hs && hs <= 0x2b07) {
returnValue = YES;
} else if (0x2934 <= hs && hs <= 0x2935) {
returnValue = YES;
} else if (0x3297 <= hs && hs <= 0x3299) {
returnValue = YES;
} else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b || hs == 0x2b50) {
returnValue = YES;
}
}
}];
return returnValue;
}
//1、首先上判断字符串是否含有表情符号的代码
/**
* 判断字符串中是否存在emoji
* @param string 字符串
* @return YES(含有表情)
*/
+ (BOOL)hasEmoji:(NSString*)string{
NSString *pattern = @"[^\\u0020-\\u007E\\u00A0-\\u00BE\\u2E80-\\uA4CF\\uF900-\\uFAFF\\uFE30-\\uFE4F\\uFF00-\\uFFEF\\u0080-\\u009F\\u2000-\\u201f\r\n]";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
BOOL isMatch = [pred evaluateWithObject:string];
return isMatch;
}
//2、然后是去除字符串中的表情
//去除字符串中所带的表情
+ (NSString *)disable_emoji:(NSString *)text{
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[^\\u0020-\\u007E\\u00A0-\\u00BE\\u2E80-\\uA4CF\\uF900-\\uFAFF\\uFE30-\\uFE4F\\uFF00-\\uFFEF\\u0080-\\u009F\\u2000-\\u201f\r\n]"options:NSRegularExpressionCaseInsensitive error:nil];
NSString *modifiedString = [regex stringByReplacingMatchesInString:text
options:0
range:NSMakeRange(0, [text length])
withTemplate:@""];
return modifiedString;
}
//3、由于苹果系统自带的九宫格输入汉字的过程中也会默认是表情符号,因此需要添加判断当前是否是九宫格键盘
/**
判断是不是九宫格
@param string 输入的字符
@return YES(是九宫格拼音键盘)
*/
+(BOOL)isNineKeyBoard:(NSString *)string{
NSString *other = @"➋➌➍➎➏➐➑➒";
int len = (int)string.length;
for(int i=0;i<len;i++){
if(!([other rangeOfString:string].location != NSNotFound))
return NO;
}
return YES;
}
+ (NSString *)string:(UITextField *)textField WithMaxLength:(CGFloat)maxLength{
NSString *toBeString = textField.text;
//获取高亮部分
UITextRange *selectedRange = [textField markedTextRange];
UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
if (!position || !selectedRange)
{
if (toBeString.length > maxLength)
{
NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:maxLength];
if (rangeIndex.length == 1)
{
return [toBeString substringToIndex:maxLength];
}
else
{
NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, maxLength)];
return [toBeString substringWithRange:rangeRange];
}
}
}
return toBeString;
}
@end
1.限制表情包
常用的是textView 和 textField,大同小异,用法不重复了。
UITextView
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
if ([textView isFirstResponder]) {
//判断键盘是不是九宫格键盘
if ([MyTextViewAndTextFieldManager isNineKeyBoard:text] ){
return YES;
}else{
if ([MyTextViewAndTextFieldManager hasEmoji:text] || [MyTextViewAndTextFieldManager stringContainsEmoji:text]){
// [MyToastManager showMsg:@"输入错误"]; // 提示框
return NO;
}
}
if ([[[textView textInputMode] primaryLanguage] isEqualToString:@"emoji"] || ![[textView textInputMode] primaryLanguage]){
// [MyToastManager showMsg:@"输入错误"]; // 提示框
return NO;
}
}
if ([text isEqualToString:@"\n"]) {
[textView resignFirstResponder];
return NO;
}
return YES;
}
- (void)textViewDidChange:(UITextView *)textView{
if (textView.text.length > 0 && [MyTextViewAndTextFieldManager stringContainsEmoji:textView.text]) {
// [MyToastManager showMsg:@"输入错误"]; // 提示框
NSLog(@"有表情");
// 禁止系统表情的输入
NSString *text = [MyTextViewAndTextFieldManager disable_emoji:[textView text]];
if (![text isEqualToString:textView.text]) {
NSRange textRange = [textView selectedRange];
textView.text = text;
[textView setSelectedRange:textRange];
}
}
}
-(void)textViewDidEndEditing:(UITextView *)textView{
if ([MyTextViewAndTextFieldManager stringContainsEmoji:textView.text]) {
// [MyToastManager showMsg:@"禁止输入表情,请重新输入"];
NSLog(@"有表情");
}
}
UITextField
- (void)textFieldDidEndEditing:(UITextField *)textField{
if ([MyTextViewAndTextFieldManager stringContainsEmoji:textField.text]) {
// [MyToastManager showMsg:@"禁止输入表情,请重新输入"];
NSLog(@"有表情");
}
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
if ([textField isFirstResponder]) {
//判断键盘是不是九宫格键盘
if ([MyTextViewAndTextFieldManager isNineKeyBoard:string] ){
return YES;
}else{
if ([MyTextViewAndTextFieldManager hasEmoji:string] || [MyTextViewAndTextFieldManager stringContainsEmoji:string]){
// [MyToastManager showMsg:@"输入错误"];
return NO;
}
}
if ([[[textField textInputMode] primaryLanguage] isEqualToString:@"emoji"] || ![[textField textInputMode] primaryLanguage]) {
// [MyToastManager showMsg:@"输入错误"];
return NO;
}
}
if ([string isEqualToString:@"\n"]) {
[textField resignFirstResponder];
return NO;
}
return YES;
}
- (void)textFieldDidChange:(UITextField *)TextField{
if (TextField.text.length > 0 && [MyTextViewAndTextFieldManager stringContainsEmoji:TextField.text]) {
// [MyToastManager showMsg:@"输入错误"];
NSLog(@"有表情");
// 禁止系统表情的输入
NSString *text = [MyTextViewAndTextFieldManager disable_emoji:[TextField text]];
TextField.text = text;
}
}
2.限制最大字符
这里是结束的时候限制,不允许输入了,截取前面的字符。
注意中文和英文字节数是不同的,所以限制字符灵活算一下,中文是两个字节。
- (void)textFieldDidEndEditing:(UITextField *)textField{
textField.text = [MyTextViewAndTextFieldManager string:textField WithMaxLength:20];
}
3.过滤特殊字符
这里用的正则,在输入时允许输入或限制输入:文章来源:https://www.toymoban.com/news/detail-443677.html
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
if ([textField isFirstResponder]) {
//判断键盘是不是九宫格键盘
if ([MyTextViewAndTextFieldManager isNineKeyBoard:string] ){
return YES;
}else{
if ([MyTextViewAndTextFieldManager hasEmoji:string] || [MyTextViewAndTextFieldManager stringContainsEmoji:string]){
// [MyToastManager showMsg:@"输入错误"]; // 提示框
return NO;
}
}
if ([[[textField textInputMode] primaryLanguage] isEqualToString:@"emoji"] || ![[textField textInputMode] primaryLanguage]){
// [MyToastManager showMsg:@"输入错误"]; // 提示框
return NO;
}
if([self JudgeTheillegalCharacter:string]){//其他字符不允许输入
NSLog(@"输入的是特殊字符..%@",string);
return NO;
}
}
if ([string isEqualToString:@"\n"]) {
[textField resignFirstResponder];
return NO;
}
return YES;
}
//判断是否含有非法字符 yes 有 no没有
- (BOOL)JudgeTheillegalCharacter:(NSString *)content{
if(![content isNotBlank]){
return NO;
}
//提示 标签不能输入特殊字符
NSString *str =@"^[A-Za-z0-9]+$";
NSPredicate* emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", str];
if (![emailTest evaluateWithObject:content]) {
return YES;
}
return NO;
}
太健忘了,记一下,写一次的代码不会写第二次,下次copy。文章来源地址https://www.toymoban.com/news/detail-443677.html
到了这里,关于iOS输入限制表情输入、最大字符、正则过滤的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!