TextInputLimit 1.0.2

TextInputLimit 1.0.2

测试已测试
语言语言 Obj-CObjective C
许可 MIT
发布最后发布2016年11月

xuwening维护。



  • xuwening

TextInputLimit是ios上一个用于文本框输入文字长度限制的库,使用起来非常简单方便。

2、手工方式集成

将TextInputLimit文件夹直接添加到项目中即可。

使用方式

在调用需要做输入长度限制的textField或textView 对象 方法:

[textObj setValue:@4 forKey:@"limit"];

长度限制只会影响设置了limit属性的对象,没有设置limit属性的对象不受影响。

使用过程中不需要对UITextField和UITextView或Xib文件做任何修改,也不需要引用头文件。

扩展(可选项)

如果需要在输入限制的同时进行一些额外的处理,例如:提示用户输入文字过多,或者添加一些动画特效等,可以注册acceptLimitLength通知。

[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(textLimitLenght:)
                                             name:@"acceptLimitLength" 
                                           object:nil];


-(void) textLimitLenght: (NSNotification *) notification {

    NSObject *object = notification.object;

    if ([object isEqual: self.textview]) {
        //收到来自textview的输入限制
    }

    if ([object isEqual: self.textfield]) {
        //收到来自textfield的输入限制
    }
    //提示
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" 
                                                    message:@"您输入的长度过长,自动被截断。"
                                                   delegate:self
                                          cancelButtonTitle:@"确定"
                                          otherButtonTitles:nil, nil];
    [alert show];
}

运行demo

git clone https://github.com/xuwening/textInputLimit.git

cd textInputLimit && pod install

打开TextInputLimitTest.xcworkspace运行。