输入框过滤操作

定义过滤类型

typedef NS_OPTIONS(NSUInteger, FilterActionType) {

    FilterKeyWordsType = 0x001,

    FilterEmojiType = 0x010,

    FilterLimitType = 0x100,

    FilterNoneType = 0x000

};

扩展UIVIew

@interface UIView (FilterKeyWords)

@property (nonatomic, strong) NSArray *filterKeyWordsArray;

@property (nonatomic, assign) int limitInputWords;

@property (nonatomic, assign) int filterActionType;

@end

执行操作


#import <objc/runtime.h>

static const char LimitInputWordsCountAddressKey;

static const char FilterKeyWordsAddressKey;

static const char FilterActionTypeAddressKey;

@implementation UIView (FilterKeyWords)

- (void)setFilterActionType:(int)filterActionType {

    objc_setAssociatedObject(self, &FilterActionTypeAddressKey, [NSString stringWithFormat:@"%d",filterActionType], OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    
    if(filterActionType == FilterNoneType) {
        return;
    }
    
    [self registerFilterNotification];
}

- (void)registerFilterNotification {

    NSString *textDidChangeNotificationName = ([self isKindOfClass:[UITextField class]] && ![(UITextField *)self isSecureTextEntry]) ?
    
    UITextFieldTextDidChangeNotification : [self isKindOfClass:[UITextView class]] ?
    
    UITextViewTextDidChangeNotification : nil;
    
    if(!textDidChangeNotificationName) {
    
        return;
    
    }
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textDidChangeAction:) name:textDidChangeNotificationName object:nil];

}

- (int)filterActionType {
    return [[self filterActionTypeString] intValue];
}

- (NSString *)filterActionTypeString {
    return objc_getAssociatedObject(self, &FilterActionTypeAddressKey);

}

- (void)setLimitInputWords:(int)limitInputWords {

    if(limitInputWords <= 0) {
        return;
    }
    
    objc_setAssociatedObject(self, &LimitInputWordsCountAddressKey, [NSString stringWithFormat:@"%d",limitInputWords], OBJC_ASSOCIATION_RETAIN_NONATOMIC);

}

- (int)limitInputWords {
    return [objc_getAssociatedObject(self, &LimitInputWordsCountAddressKey) intValue];

}

- (NSArray *)filterKeyWordsArray {
    return objc_getAssociatedObject(self, &FilterKeyWordsAddressKey);

}

- (void)setFilterKeyWordsArray:(NSArray *)filterKeyWordsArray {
    if(filterKeyWordsArray) {
        objc_setAssociatedObject(self, &FilterKeyWordsAddressKey, filterKeyWordsArray, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }

}

- (void)textDidChangeAction:(NSNotification *)notify {

    NSString *selfText = [self valueForKey:@"text"];
    
    NSString *lang = [self.textInputMode primaryLanguage]; // 键盘输入模式
    
    int limitCount = self.limitInputWords;
    
    FilterActionType actionType = [[self filterActionTypeString] intValue];
    
    if ([lang isEqualToString:@"zh-Hans"]) { // 简体中文输入,包括简体拼音,健体五笔,简体手写
    
        UITextRange *selectedRange = [self valueForKey:@"markedTextRange"];
        
        //获取高亮部分
        
        UITextPosition *position = nil;
        
        if([self isKindOfClass:[UITextField class]]) {
        
            position = [(UITextField *)self positionFromPosition:selectedRange.start offset:0];
        
        }

        else if ([self isKindOfClass:[UITextView class]]) {
        
            position = [(UITextView *)self positionFromPosition:selectedRange.start offset:0];
        
        }

    // 没有高亮选择的字,则对已输入的文字进行字数统计和限制
    
        if (!position) {
        
            if (actionType & FilterLimitType && selfText.length > limitCount) {
            
                selfText = [selfText substringToIndex:limitCount];
            
            }
        
            [self setValue:[self textFilterWordsWithText:selfText] forKey:@"text"];
        
        }
        
        // 有高亮选择的字符串,则暂不对文字进行统计和限制
        
        else{
            
        }
    
    }
    
    // 中文输入法以外的直接对其统计限制即可,不考虑其他语种情况
    
    else{
    
        if (actionType & FilterLimitType && selfText.length > limitCount) {
        
            selfText = [selfText substringToIndex:limitCount];
        
        }
    
        [self setValue:[self textFilterWordsWithText:selfText] forKey:@"text"];
    
    }

}
// 过滤掉文字
- (NSString *)textFilterWordsWithText:(NSString *)aText {

    NSString *tempString = aText;
    
    FilterActionType actionType = [[self filterActionTypeString] intValue];
    
    if(actionType == FilterNoneType) {
    
        return aText;
    
    }
    
    if(actionType & FilterKeyWordsType) {
    
        __block NSString *replaceString = tempString;
        
        [self.filterKeyWordsArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        
            if([obj isKindOfClass:[NSString class]] && [obj length] > 0) {
        
            replaceString = [replaceString stringByReplacingOccurrencesOfString:obj withString:@""];
        
        }
        
        }];
    
        tempString = replaceString;
    
    }
    
    if(actionType & FilterEmojiType) {
    
        tempString = [tempString stringByReplacingOccurrencesOfString:@"[^\\u0020-\\u007E\\u00A0-\\u00BE\\u2E80-\\uA4CF\\uF900-\\uFAFF\\uFE30-\\uFE4F\\uFF00-\\uFFEF\\u0080-\\u009F\\u2000-\\u201f]" withString:@"" options:NSRegularExpressionSearch range:NSMakeRange(0, [tempString length])];
    
    }
    
    return tempString;

}

@end

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,267评论 4 61
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,099评论 19 139
  • 正如 这偶尔的饥馁 水 蒸发于无形 从肉体中渐渐抽出 你看见自己 远处 塔照见在泥潭 伞 映出风雨 青黄一片的 你...
    四夕山人阅读 252评论 0 1
  • 第一章《接单》 “ 我的世界里,顾客不是我们的上帝,对于我来说,我们才是顾客的上帝。” “好”,收工...
    小压力阅读 215评论 0 0
  • 当我看到罗兰罗曼的“世上只有一种英雄主义,就是在认清生活的真相之后,依然热爱生活”时,我脑海里涌现的第一个人,便是...
    大牙惠阅读 13,449评论 51 206