输入框过滤操作
2016-08-15 本文已影响51人
xgou
定义过滤类型
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