首页 > 代码库 > 自定义UItextFiled,限制TextFiled 的输入长度

自定义UItextFiled,限制TextFiled 的输入长度

1、一般情况下,处理textFiled的字符数都会用到textField的代理方法

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;

在方法中对字符数进行控制,但是此方法的有一定的问题。现在推出一种新的自定义的机制,自定义textField控件,为其增加一个控制输入长度的属性,如下

.h文件

#import <UIKit/UIKit.h>

@interface PKTextField : UITextField

@property (nonatomic,assign)NSInteger allowInputLength;

@end

.m 文件

#import "PKTextField.h"

@implementation PKTextField

- (instancetype)init{
    self = [super init];
    if (self) {
        self.allowInputLength = CGFLOAT_MAX;
        [self addTarget:self action:@selector(textFieldEditChanged) forControlEvents:UIControlEventEditingChanged];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    self.allowInputLength = CGFLOAT_MAX;
    [self addTarget:self action:@selector(textFieldEditChanged) forControlEvents:UIControlEventEditingChanged];
    return self;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    self.clipsToBounds = YES;
    self.backgroundColor = [UIColor whiteColor];
    self.layer.cornerRadius = 2.5;
    self.layer.borderWidth = 0.5;
    self.borderStyle = UITextBorderStyleNone;
}

-(void)setEnabled:(BOOL)enabled{
    [super setEnabled:enabled];
    
    if (enabled == YES) {
        self.layer.borderColor = [UIColor colorFromHexRGB:@"999999" alpha:1.0].CGColor;
        
        CGRect frame = self.frame;
        frame.size.width = 5.0;
        UIView *leftview = [[UIView alloc] initWithFrame:frame];
        self.leftViewMode = UITextFieldViewModeAlways;
        self.leftView = leftview;

        CGRect rightFrame = CGRectMake(0,0,5, self.bounds.size.height);
        UIView * rightView = [[UIView alloc] initWithFrame:rightFrame];
        self.rightViewMode = UITextFieldViewModeAlways;
        self.rightView = rightView;
       
        
    }else{
        self.layer.borderColor = [UIColor clearColor].CGColor;
        self.leftView = nil;
        self.rightView = nil;
    }
}

-(void)textFieldEditChanged
{
    UITextField *textField = self;
    NSString *toBeString = textField.text;
    
    //获取高亮部分
    UITextRange *selectedRange = [textField markedTextRange];
    UITextPosition *position = [textField positionFromPosition:selectedRange.start offset:0];
    
    // 没有高亮选择的字,则对已输入的文字进行字数统计和限制
    if (!position)
    {
        if (toBeString.length > self.allowInputLength)
        {
            NSRange rangeIndex = [toBeString rangeOfComposedCharacterSequenceAtIndex:self.allowInputLength];
            if (rangeIndex.length == 1)
            {
                textField.text = [toBeString substringToIndex:self.allowInputLength];
            }
            else
            {
                NSRange rangeRange = [toBeString rangeOfComposedCharacterSequencesForRange:NSMakeRange(0, self.allowInputLength)];
                textField.text = [toBeString substringWithRange:rangeRange];
            }
        }
    }
}


@end

使用示例:

PKTextField * titleTF = [[PKTextField alloc] initWithFrame:CGRectMake(leftPadding, 17, ScreenWidth - leftPadding - BtnHotWidth, 30)]; titleTF.textColor = [UIColor blackColor]; 
titleTF.enabled
= NO;
titleTF.text
= @"";
titleTF.font
= [UIFont systemFontOfSize:23.0];
titleTF.allowInputLength
= 10;

 

自定义UItextFiled,限制TextFiled 的输入长度