- (BOOL)textFieldShouldReturn:(UITextField *)textField{
//把表情轉(zhuǎn)換為數(shù)字 因?yàn)樵试S666的出現(xiàn) 所以表情的6轉(zhuǎn)化為0
NSString *str = [textField.text stringByReplacingEmojiUnicodeWithCheatCodes];
//判斷是否連續(xù)的6
NSString *regex1 = @"[6]*";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex1];
if ([pred evaluateWithObject:str]) {
NSLog(@"發(fā)送im");
textField.text = @"";
return YES;
}
//判斷單個(gè)數(shù)字
NSString *regex2= @"[0123456789]*";
NSPredicate *pred2 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
if ([pred2 evaluateWithObject:str]) {
NSLog(@"限制");
[textField resignFirstResponder];
textField.text = @"";
return YES;
}
//判斷輸入數(shù)字總數(shù)大于6位
NSUInteger num = 0;
NSString *pattern = @"[0123456789]\\d*";
NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern: pattern options: NSRegularExpressionCaseInsensitive error: &error];
NSArray *match = [regex matchesInString: str options: NSMatchingCompleted range: NSMakeRange(0, [str length])];
for (NSTextCheckingResult *result in match) {
num = num+ result.range.length;
}
//數(shù)字是否超過(guò)限制
if (num >= 6) {
NSLog(@"超過(guò)限制");
}else{
NSLog(@"未超過(guò)限制");
}
[textField resignFirstResponder];
textField.text = @"";
return YES;
}
里面用到一個(gè)分類(lèi)的方法stringByReplacingEmojiUnicodeWithCheatCodes
參考自 https://github.com/vbonluk/iOS_Emoji
然后我修改了要轉(zhuǎn)換的東西
//
// NSString+Emoji.h
//
//
// Created by Valerio Mazzeo on 24/04/13.
// Copyright (c) 2013 Valerio Mazzeo. All rights reserved.
//
/**
NSString (Emoji) extends the NSString class to provide custom functionality
related to the Emoji emoticons.
Through this category, it is possible to turn cheat codes from
Emoji Cheat Sheet <http://www.emoji-cheat-sheet.com> into unicode emoji characters
and vice versa (useful if you need to POST a message typed by the user to a remote service).
*/
@interface NSString (Emoji)
/**
Returns a NSString in which any occurrences that match the cheat codes
from Emoji Cheat Sheet <http://www.emoji-cheat-sheet.com> are replaced by the
corresponding unicode characters.
Example:
"This is a smiley face :smiley:"
Will be replaced with:
"This is a smiley face \U0001F604"
*/
- (NSString *)stringByReplacingEmojiCheatCodesWithUnicode;
/**
Returns a NSString in which any occurrences that match the unicode characters
of the emoji emoticons are replaced by the corresponding cheat codes from
Emoji Cheat Sheet <http://www.emoji-cheat-sheet.com>.
Example:
"This is a smiley face \U0001F604"
Will be replaced with:
"This is a smiley face :smiley:"
*/
- (NSString *)stringByReplacingEmojiUnicodeWithCheatCodes;
@end
//
// NSString+Emoji.m
//
//
// Created by Valerio Mazzeo on 24/04/13.
// Copyright (c) 2013 Valerio Mazzeo. All rights reserved.
//
#import "NSString+Emoji.h"
@implementation NSString (Emoji)
static NSDictionary * s_unicodeToCheatCodes = nil;
static NSDictionary * s_cheatCodesToUnicode = nil;
+ (void)initializeEmojiCheatCodes
{
NSDictionary *forwardMap = @{
@"0??": @"0",
@"1??": @"1",
@"2??": @"2",
@"3??": @"3",
@"4??": @"4",
@"5??": @"5",
@"6??": @"0",
@"7??": @"7",
@"8??": @"8",
@"9??": @"9",
@"①": @"1",
@"②": @"2",
@"③": @"3",
@"④": @"4",
@"⑤": @"5",
@"⑥": @"0",
@"⑦": @"7",
@"⑧": @"8",
@"⑨": @"9",
@"一": @"1",
@"二": @"2",
@"三": @"3",
@"四": @"4",
@"五": @"5",
@"六": @"0",
@"七": @"7",
@"八": @"8",
@"九": @"9"
};
NSMutableDictionary *reversedMap = [NSMutableDictionary dictionaryWithCapacity:[forwardMap count]];
[forwardMap enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
if ([obj isKindOfClass:[NSArray class]]) {
for (NSString *object in obj) {
[reversedMap setObject:key forKey:object];
}
} else {
[reversedMap setObject:key forKey:obj];
}
}];
@synchronized(self) {
s_unicodeToCheatCodes = forwardMap;
s_cheatCodesToUnicode = [reversedMap copy];
}
}
- (NSString *)stringByReplacingEmojiCheatCodesWithUnicode
{
if (!s_cheatCodesToUnicode) {
[NSString initializeEmojiCheatCodes];
}
if ([self rangeOfString:@":"].location != NSNotFound) {
__block NSMutableString *newText = [NSMutableString stringWithString:self];
[s_cheatCodesToUnicode enumerateKeysAndObjectsUsingBlock:^(NSString *key, id obj, BOOL *stop) {
[newText replaceOccurrencesOfString:key withString:obj options:NSLiteralSearch range:NSMakeRange(0, newText.length)];
}];
return newText;
}
return self;
}
- (NSString *)stringByReplacingEmojiUnicodeWithCheatCodes
{
if (!s_cheatCodesToUnicode) {
[NSString initializeEmojiCheatCodes];
}
if (self.length) {
__block NSMutableString *newText = [NSMutableString stringWithString:self];
[s_unicodeToCheatCodes enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
NSString *string = ([obj isKindOfClass:[NSArray class]] ? [obj firstObject] : obj);
[newText replaceOccurrencesOfString:key withString:string options:NSLiteralSearch range:NSMakeRange(0, newText.length)];
}];
return newText;
}
return self;
}
@end