定義過濾類型
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
執(zhí)行操作
#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];
}
// 沒有高亮選擇的字砸王,則對已輸入的文字進行字數(shù)統(tǒng)計和限制
if (!position) {
if (actionType & FilterLimitType && selfText.length > limitCount) {
selfText = [selfText substringToIndex:limitCount];
}
[self setValue:[self textFilterWordsWithText:selfText] forKey:@"text"];
}
// 有高亮選擇的字符串谜喊,則暫不對文字進行統(tǒng)計和限制
else{
}
}
// 中文輸入法以外的直接對其統(tǒng)計限制即可既穆,不考慮其他語種情況
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
最后編輯于 :
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者