美文网首页
聊天限制输入邀请码数字

聊天限制输入邀请码数字

作者: Jody526 | 来源:发表于2018-06-28 11:25 被阅读20次
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    //把表情转换为数字 因为允许666的出现 所以表情的6转化为0
    NSString *str = [textField.text stringByReplacingEmojiUnicodeWithCheatCodes];
    
    //判断是否连续的6
    NSString *regex1 = @"[6]*";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex1];
    if ([pred evaluateWithObject:str]) {
        NSLog(@"发送im");
        textField.text = @"";
        return YES;
    }
    
    //判断单个数字
    NSString *regex2= @"[0123456789]*";
    NSPredicate *pred2 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
    if ([pred2 evaluateWithObject:str]) {
        NSLog(@"限制");
        [textField resignFirstResponder];
        textField.text = @"";
        return YES;
    }
    
    //判断输入数字总数大于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;
    }
    
    //数字是否超过限制
    if (num >= 6) {
        NSLog(@"超过限制");
    }else{
      NSLog(@"未超过限制");
    }
    
    [textField resignFirstResponder];
    textField.text = @"";
    return YES;
}

里面用到一个分类的方法stringByReplacingEmojiUnicodeWithCheatCodes

参考自 https://github.com/vbonluk/iOS_Emoji

然后我修改了要转换的东西

//
//  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

相关文章

网友评论

      本文标题:聊天限制输入邀请码数字

      本文链接:https://www.haomeiwen.com/subject/isfgyftx.html