ios 中文英文字符集

+ (NSCharacterSet *) chineseAndEngSet
{
    if (chineseNameSet == nil)
    {
        NSMutableCharacterSet *aCharacterSet = [[NSMutableCharacterSet alloc] init];
        
        NSRange lcEnglishRange;
        lcEnglishRange.location = (unsigned int)0x4e00;
        lcEnglishRange.length = (unsigned int)0x9fa5 - (unsigned int)0x4e00;
        [aCharacterSet addCharactersInRange:lcEnglishRange];
        [aCharacterSet addCharactersInString:@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"];
        chineseAndEngSet = aCharacterSet;
    }
    return chineseAndEngSet;
}

中文字符的正則表達式(包含繁體中文) ^[\u4E00-\u9FA5]+$ 正則表達式

驗證中文 spa

- (BOOL)validateInputWithString:(NSString *)aString
{
    NSString * const regularExpression = @"^[\u4E00-\u9FA5]+$";
    NSError *error = NULL;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regularExpression
                                                                           options:NSRegularExpressionCaseInsensitive
                                                                             error:&error];
    if (error) {
        NSLog(@"error %@", error);
    }

    NSUInteger numberOfMatches = [regex numberOfMatchesInString:aString
                                                        options:0
                                                          range:NSMakeRange(0, [aString length])];
    return numberOfMatches > 0;
}

判斷雙空格方法 code

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{

 //Check for double space
 return !(range.location > 0 &&
          [string length] > 0 &&
          [[NSCharacterSet whitespaceCharacterSet] characterIsMember:[string characterAtIndex:0]] &&
          [[NSCharacterSet whitespaceCharacterSet] characterIsMember:[[textField text] characterAtIndex:range.location - 1]]);

}
相關文章
相關標籤/搜索