emoji ios 表情获取
2018-01-29 14:59:00 weixin_34163741 阅读数 4

在提交数据发送网络请求,由于用户输入了emoji表情,服务端返回系统异常,体验感很差。为了解决服务器不能验证emoji编码的问题,需要在本地进行emoji的输入控制(一般情况应该由服务器在数据库中添加emoji对应的转码表以支持客户端发送emoji表情)。

所以就有了这个需求:在TextField或者TextView中,限制输入任何表情符号,(包括系统自带的表情,第三方键盘带的表情,还有某些汉字类型的表情)。

利用下面这个方法stringContainsEmoji可以限制系统键盘自带的表情

/**
*  判断字符串中是否存在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;
}

利用下面这个方法hasEmoji可以限制第三方键盘(常用的是搜狗键盘)的表情

/**
*  判断字符串中是否存在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;
}

利用下面这个方法hasEmoji可以限制第三方键盘(常用的是搜狗键盘)的表情

/**
*  判断字符串中是否存在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;
}

利用下面这个方法isNineKeyBoard可以判断当前是不是在使用九宫格输入

/**
判断是不是九宫格
@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;
}

下面就是现在UITextView禁止输入表情的重要代码,记得遵守代理~

#pragma mark -------UITextViewDelegate
-(BOOL) textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
   if ([textView isFirstResponder]) {
       
       if ([[[textView textInputMode] primaryLanguage] isEqualToString:@"emoji"] || ![[textView textInputMode] primaryLanguage]) {
           return NO;
       }

       //判断键盘是不是九宫格键盘
       if ([self isNineKeyBoard:text] ){
               return YES;
       }else{
           if ([self hasEmoji:text] || [GeneralMethods stringContainsEmoji:text]){
               return NO;
           }
       }
   }
   return YES;
}

UITextField在下面这个方法中写就行

#pragma mark -----UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{

}

另外附上几个有用的方法:

//-----过滤字符串中的emoji
- (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;
}
/**
* 判断 字母、数字、中文
*/
- (BOOL)isInputRuleAndNumber:(NSString *)str
{
   NSString *other = @"➋➌➍➎➏➐➑➒";     //九宫格的输入值
   unsigned long len=str.length;
   for(int i=0;i<len;i++)
   {
       unichar a=[str characterAtIndex:i];
       if(!((isalpha(a))
            ||(isalnum(a))
//             ||((a=='_') || (a == '-')) //判断是否允许下划线,昵称可能会用上
            ||((a==' '))                 //判断是否允许空格
            ||((a >= 0x4e00 && a <= 0x9fa6))
            ||([other rangeOfString:str].location != NSNotFound)
            ))
           return NO;
   }
   return YES;
}

注意:上面这种方法可以限制键盘输入任何表情符号,但是如果需求是必须要输入表情,而且要下次服务器把这个表情返回,还要回显到页面上,就要看下面这篇文章

转载于:https://www.jianshu.com/p/2f5bb0a419fa

2017-04-11 09:39:00 weixin_34126557 阅读数 24

很多时候我们需要判断用户输入的文字是否是表情,因为有些地方不允许用户输入表情,例如名字,昵称,个性签名等。网上也有很多方法检测,但大部分都是有漏洞的。所以在这里贴上一段代码,亲测有用测试输入的表情都能判断出来,如果大家用的有问题,敬请相告,改之。谢谢!

- (BOOL)isEmoji:(NSString *)string
{
    if ([string length]<2)
    {
        return NO;
    }

    static NSCharacterSet *_variationSelectors;
    _variationSelectors = [NSCharacterSet characterSetWithRange:NSMakeRange(0xFE00, 16)];

    if ([string rangeOfCharacterFromSet: _variationSelectors].location != NSNotFound) 
    {
        return YES;
    }
    const unichar high = [string characterAtIndex:0];
    // Surrogate pair (U+1D000-1F9FF)
    if (0xD800 <= high && high <= 0xDBFF) 
    {
        const unichar low = [string characterAtIndex: 1];
        const int codepoint = ((high - 0xD800) * 0x400) + (low - 0xDC00) + 0x10000;
        return (0x1D000 <= codepoint && codepoint <= 0x1F9FF);
        // Not surrogate pair (U+2100-27BF)
    } 
    else 
    {
        return (0x2100 <= high && high <= 0x27BF);
    }
}
转载请注明出处,谢谢!
2018-01-29 14:59:00 weixin_34344677 阅读数 8

在提交数据发送网络请求,由于用户输入了emoji表情,服务端返回系统异常,体验感很差。为了解决服务器不能验证emoji编码的问题,需要在本地进行emoji的输入控制(一般情况应该由服务器在数据库中添加emoji对应的转码表以支持客户端发送emoji表情)。

所以就有了这个需求:在TextField或者TextView中,限制输入任何表情符号,(包括系统自带的表情,第三方键盘带的表情,还有某些汉字类型的表情)。

利用下面这个方法stringContainsEmoji可以限制系统键盘自带的表情

/**
*  判断字符串中是否存在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;
}

利用下面这个方法hasEmoji可以限制第三方键盘(常用的是搜狗键盘)的表情

/**
*  判断字符串中是否存在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;
}

利用下面这个方法hasEmoji可以限制第三方键盘(常用的是搜狗键盘)的表情

/**
*  判断字符串中是否存在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;
}

利用下面这个方法isNineKeyBoard可以判断当前是不是在使用九宫格输入

/**
判断是不是九宫格
@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;
}

下面就是现在UITextView禁止输入表情的重要代码,记得遵守代理~

#pragma mark -------UITextViewDelegate
-(BOOL) textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
   if ([textView isFirstResponder]) {
       
       if ([[[textView textInputMode] primaryLanguage] isEqualToString:@"emoji"] || ![[textView textInputMode] primaryLanguage]) {
           return NO;
       }

       //判断键盘是不是九宫格键盘
       if ([self isNineKeyBoard:text] ){
               return YES;
       }else{
           if ([self hasEmoji:text] || [GeneralMethods stringContainsEmoji:text]){
               return NO;
           }
       }
   }
   return YES;
}

UITextField在下面这个方法中写就行

#pragma mark -----UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{

}

另外附上几个有用的方法:

//-----过滤字符串中的emoji
- (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;
}
/**
* 判断 字母、数字、中文
*/
- (BOOL)isInputRuleAndNumber:(NSString *)str
{
   NSString *other = @"➋➌➍➎➏➐➑➒";     //九宫格的输入值
   unsigned long len=str.length;
   for(int i=0;i<len;i++)
   {
       unichar a=[str characterAtIndex:i];
       if(!((isalpha(a))
            ||(isalnum(a))
//             ||((a=='_') || (a == '-')) //判断是否允许下划线,昵称可能会用上
            ||((a==' '))                 //判断是否允许空格
            ||((a >= 0x4e00 && a <= 0x9fa6))
            ||([other rangeOfString:str].location != NSNotFound)
            ))
           return NO;
   }
   return YES;
}

注意:上面这种方法可以限制键盘输入任何表情符号,但是如果需求是必须要输入表情,而且要下次服务器把这个表情返回,还要回显到页面上,就要看下面这篇文章

转载于:https://www.jianshu.com/p/2f5bb0a419fa

2019-01-12 15:02:50 allanGold 阅读数 178

在提交数据发送网络请求,由于用户输入了emoji表情,服务端返回系统异常,体验感很差。为了解决服务器不能验证emoji编码的问题,需要在本地进行emoji的输入控制(一般情况应该由服务器在数据库中添加emoji对应的转码表以支持客户端发送emoji表情)。

所以就有了这个需求:在TextField或者TextView中,限制输入任何表情符号,(包括系统自带的表情,第三方键盘带的表情,还有某些汉字类型的表情)。

利用下面这个方法stringContainsEmoji可以限制系统键盘自带的表情

/**
*  判断字符串中是否存在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;
}

利用下面这个方法hasEmoji可以限制第三方键盘(常用的是搜狗键盘)的表情

/**
*  判断字符串中是否存在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;
}

利用下面这个方法hasEmoji可以限制第三方键盘(常用的是搜狗键盘)的表情

/**
*  判断字符串中是否存在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;
}

利用下面这个方法isNineKeyBoard可以判断当前是不是在使用九宫格输入

/**
判断是不是九宫格
@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;
}

下面就是现在UITextView禁止输入表情的重要代码,记得遵守代理~

#pragma mark -------UITextViewDelegate
-(BOOL) textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
   if ([textView isFirstResponder]) {
       
       if ([[[textView textInputMode] primaryLanguage] isEqualToString:@"emoji"] || ![[textView textInputMode] primaryLanguage]) {
           return NO;
       }

       //判断键盘是不是九宫格键盘
       if ([self isNineKeyBoard:text] ){
               return YES;
       }else{
           if ([self hasEmoji:text] || [GeneralMethods stringContainsEmoji:text]){
               return NO;
           }
       }
   }
   return YES;
}

UITextField在下面这个方法中写就行

#pragma mark -----UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{

}

另外附上几个有用的方法:

//-----过滤字符串中的emoji
- (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;
}
/**
* 判断 字母、数字、中文
*/
- (BOOL)isInputRuleAndNumber:(NSString *)str
{
   NSString *other = @"➋➌➍➎➏➐➑➒";     //九宫格的输入值
   unsigned long len=str.length;
   for(int i=0;i<len;i++)
   {
       unichar a=[str characterAtIndex:i];
       if(!((isalpha(a))
            ||(isalnum(a))
//             ||((a=='_') || (a == '-')) //判断是否允许下划线,昵称可能会用上
            ||((a==' '))                 //判断是否允许空格
            ||((a >= 0x4e00 && a <= 0x9fa6))
            ||([other rangeOfString:str].location != NSNotFound)
            ))
           return NO;
   }
   return YES;
}

注意:上面这种方法可以限制键盘输入任何表情符号,但是如果需求是必须要输入表情,而且要下次服务器把这个表情返回,还要回显到页面上,就要看下面这篇文章


链接:https://www.jianshu.com/p/2f5bb0a419fa
 

2019-04-04 17:49:00 weixin_34198797 阅读数 3

UITextField

self.remarkTextField.delegate = self;
numLim = 20;

#pragma mark - TextFieldDelegate

//限制字数输入
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

    if (textField == self.remarkTextField) {
        //这里的if时候为了获取删除操作,如果没有次if会造成当达到字数限制后删除键也不能使用的后果.
        if (range.length == 1 && string.length == 0) {
            return YES;
        } else if (self.remarkTextField.text.length >= numLim) {
            self.remarkTextField.text = [textField.text substringToIndex:numLim];
            return NO;
        }
    }
    return YES;
}

/**********过滤表情emoji************/
//添加通知检测UITextField
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeText:) name:UITextFieldTextDidChangeNotification object:nil];

//过滤方法
- (void)changeText:(UITextField*)sender{
    NSLog(@"没有过滤前输入的字符串===%@",self.remarkTextField.text);
    NSString *filterString = [self filterEmoji:self.remarkTextField.text];
    NSLog(@"过滤Emoji表情后的字符串===%@",filterString);
    self.remarkTextField.text = filterString;
    _strLengthLbl.text = [NSString stringWithFormat:@"%lu/%d",_remarkTextField.text.length,numLim];
}

UITextView

//限制字数
- (void)textViewDidChange:(UITextField *)textView{
    if (textView.text.length > 20) {
        textView.text = [textView.text substringToIndex:20];
    }
    _strLengthLbl.text = [NSString stringWithFormat:@"%lu/50",textView.text.length];
}

//UITextView有直接可用代理进行控制过滤表情emoji
- (void)textViewDidChange:(UITextView *)textView{
    NSLog(@"没有过滤前输入的字符串===%@",textView.text);
    NSString *filterString = [self filterEmoji:textView.text];
    NSLog(@"过滤Emoji表情后的字符串===%@",filterString);
    textView.text = filterString;
}

正则过滤表情

- (NSString *)filterEmoji:(NSString *)text{
    if (!text.length) return 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;
}

限制输入手机号码

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    NSString *oldText = textField.text;
    NSString *newText = [oldText stringByReplacingCharactersInRange:range withString:string];
    NSString *numtext = newText.length > 0 || range.length > 0 ? newText : oldText;
    
    if (numtext.length > 10) {
        textField.text = [numtext substringToIndex:10];
    }
    return YES;
}

移除emoji表情、判断emoji表情

博文 来自: w250130255
没有更多推荐了,返回首页