背景介绍
今天做项目时候由于有个需求是需要对textField输入框进行内容和长度的限制,然后在网上找了大量的相关问题,都没有能够解决我的需求的,所以只能自己进行完善。
代码介绍
代码完成了针对输入内容的实时监听,并且完成了和键盘配套的复制来的内容的限制,所以本套代码可以实现输入框内完整干净的限制。
代码实现
本代码是在textfield的类目中实现的,所以请需要的自行完善,或询本人。
.h
#import <UIKit/UIKit.h>
@interface UITextField (ZG_LengthLimit)
@property (nonatomic,strong)NSNumber *MAXLENGTH;//传入Integer类型的数据,
- (void)addObserverTextChangeWithMAXLENGTH:(NSInteger)MAXLENGTH;
@end
.m
#import "UITextField+ZG_LengthLimit.h"
#import <objc/runtime.h>
@implementation UITextField (ZG_LengthLimit)
- (void)setMAXLENGTH:(NSNumber *)_MAXLENGTH {
objc_setAssociatedObject(self, @"MAXLENGTH", _MAXLENGTH, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (NSNumber *)MAXLENGTH {
return objc_getAssociatedObject(self, @"MAXLENGTH");
}
- (void)addObserverTextChangeWithMAXLENGTH:(NSInteger)MAXLENGTH{
self.MAXLENGTH = [NSNumber numberWithInteger:MAXLENGTH];
[self addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
}
- (void)textFieldDidChange:(id)sender
{
UITextField *textField = self;
NSString *toBeString = textField.text;
//获取高亮部分
UITextRange *selectedRange = [textField markedTextRange];
UITextPosition *position1 = [textField positionFromPosition:selectedRange.start offset:0];
// 没有高亮选择的字,则对已输入的文字进行字数统计和限制
if (!position1 || !selectedRange)
{
NSString *patth = [self getPattern];
if (patth) {
toBeString = [self stringisMyNeed:self.text andPattern:patth];
textField.text = toBeString;
}
if (toBeString.length > [self.MAXLENGTH integerValue])
{
NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:[self.MAXLENGTH integerValue]];
if (rangeIndex.length == 1)
{
textField.text = [toBeString substringToIndex:[self.MAXLENGTH integerValue]];
}
else
{
NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, [self.MAXLENGTH integerValue])];
textField.text = [toBeString substringWithRange:rangeRange];
}
}
}
}
/*
UIKeyboardTypeDefault, all
UIKeyboardTypeASCIICapable, [0-9]|[A-Za-z]
UIKeyboardTypeNumbersAndPunctuation, [A-Za-z]|[\u4e00-\u9fa5]
UIKeyboardTypeURL, all
UIKeyboardTypeNumberPad, [0-9]
UIKeyboardTypePhonePad, [0-9]
UIKeyboardTypeNamePhonePad, all
UIKeyboardTypeEmailAddress, all
UIKeyboardTypeDecimalPad NS_ENUM_AVAILABLE_IOS(4_1), [0-9]
UIKeyboardTypeTwitter NS_ENUM_AVAILABLE_IOS(5_0), all
UIKeyboardTypeWebSearch NS_ENUM_AVAILABLE_IOS(7_0), all
UIKeyboardTypeASCIICapableNumberPad NS_ENUM_AVAILABLE_IOS(10_0), [0-9]
UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable, [0-9]|[A-Za-z]
*/
- (NSString *)getPattern{
NSString *patternStr;
switch (self.keyboardType) {
case UIKeyboardTypeDefault:
patternStr = nil;
break;
case UIKeyboardTypeASCIICapable:
patternStr = @"[0-9]|[A-Za-z]";
break;
case UIKeyboardTypeNumbersAndPunctuation:
patternStr = @"[A-Za-z]|[\u4e00-\u9fa5]";
break;
case UIKeyboardTypeURL:
patternStr = nil;
break;
case UIKeyboardTypeNumberPad:
patternStr = @"[0-9]";
break;
case UIKeyboardTypePhonePad:
patternStr = @"[0-9]";
break;
case UIKeyboardTypeNamePhonePad:
patternStr = nil;
break;
case UIKeyboardTypeEmailAddress:
patternStr = nil;
break;
case UIKeyboardTypeDecimalPad:
patternStr = @"[0-9]";
break;
case UIKeyboardTypeTwitter:
patternStr = nil;
break;
case UIKeyboardTypeWebSearch:
patternStr = nil;
break;
case UIKeyboardTypeASCIICapableNumberPad:
patternStr = @"[0-9]";
break;
default:
patternStr = nil;
break;
}
return patternStr;
}
- (NSString *)stringisMyNeed:(NSString *)str andPattern:(NSString *)pattern{
NSRange range;
for(int i=0; i<str.length; i+=range.length){
range = [str rangeOfComposedCharacterSequenceAtIndex:i];
NSString *s = [str substringWithRange:range];
BOOL isdel = [self isNeed:s WithPattern:pattern];
if (!isdel) {
str = [str stringByReplacingCharactersInRange:range withString:@""];
i-=range.length;
}
}
return str;
}
- (BOOL)isNeed:(NSString *)str WithPattern:(NSString *)pattern{
//[0-9]|[A-Za-z]|[\u4e00-\u9fa5]
NSRegularExpression *tLetterRegularExpression = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:nil];
NSUInteger tLetterMatchCount = [tLetterRegularExpression numberOfMatchesInString:str options:NSMatchingReportProgress range:NSMakeRange(0, str.length)];
NSLog(@"%ld",tLetterMatchCount);
if(tLetterMatchCount>=1){
return YES;
}else{
return NO;
}
}
以上为全部代码。
其中注释部分为对应键盘的正则,目前只做了数字,字母,汉字的。有其他需求的可以自行加入
有问题可以留言。
谢谢。
参考网址:
http://www.jianshu.com/p/2d1c06f2dfa4
http://www.cnblogs.com/sunnyke/p/4665248.html