InputKit 是一个优雅的控件,用于限制输入文本,灵感来源于 BlocksKit,由Objective-C和Swift编写。
语言
安装
有三种方式在项目中使用InputKit:
-
使用CocoaPods
-
手动
-
使用Carthage
CocoaPods
CocoaPods是Objective-C的依赖管理工具,它自动简化了在项目中使用第三方库的过程。
Podfile
platform :ios, '8.0'
Objective-C
pod 'InputKit', '~> 1.1.15'
Swift 3.x
pod 'InputKitSwift', '~> 1.1.14'
Swift 4.0
pod 'InputKitSwift', '~> 1.1.16'
手册
下载代码库的ZIP文件,然后将“InputKit”文件夹中的所有文件拖拽到你的项目中。
Carthage
Carthage 是一个去中心化的依赖管理器,可以构建你的依赖并提供二进制框架。
您可以使用以下命令通过 Homebrew 安装 Carthage:
$ brew update
$ brew install carthage
要使用 Carthage 将 InputKit 或 InputKitSwift 集成到你的 Xcode 项目中,请在 Cartfile 中指定它
github "tingxins/InputKit"
运行 carthage 构建框架,并根据需要将相应的框架(InputKit.framework 或 InputKitSwift.framework)拖拽到你的 Xcode 项目中。确保仅添加一个框架而不要同时添加两个。
使用方法
你可以运行 InputKitDemo 来获取更多细节。
使用代码
首先,在使用时导入头文件。
#import "InputKit.h"
针对UITextField
使用TXLimitedTextField类。
TXLimitedTextFieldTypeDefault
TXLimitedTextFieldTypeDefault类型的目的是限制
示例
TXLimitedTextField *textField = [[TXLimitedTextField alloc] initWithFrame:CGRectMake(20, 200, 100, 30)];
// Of course, you can ignored this line of codes. TXLimitedTextFieldTypeDefault is default.
textField.limitedType = TXLimitedTextFieldTypeDefault;
// this means, you can only input ten characters. It limits the max number of characters that you input.
textField.limitedNumber = 10;
[self.view addSubview:textField];
TXLimitedTextFieldTypePrice
TXLimitedTextFieldTypePrice类型不仅限制了输入的字符数量,还可以为您的文本输入框提供更多有用的限制。我们通常在只需输入整数或小数的TXLimitedTextField中使用它。
示例
TXLimitedTextField *textField = [[TXLimitedTextField alloc] initWithFrame:CGRectMake(20, 200, 100, 30)];
// Type
textField.limitedType = TXLimitedTextFieldTypePrice;
// you can also set the property in this type. It limits the max number of characters that you input
textField.limitedNumber = 10;
// this means, that only five ints can be inputted
textField.limitedPrefix = 5;
// this means, that only five decimal can be inputted
textField.limitedSuffix = 2;
[self.view addSubview:textField];
TXLimitedTextFieldTypeCustom
这种类型 的 TXLimitedTextFieldTypeCustom 很有趣。您可以使用正则表达式自定义自己的TXLimitedTextField,但需要注意使用时的一些注意事项。
示例
TXLimitedTextField *textField = [[TXLimitedTextField alloc] initWithFrame:CGRectMake(20, 200, 100, 30)];
// Of course, you can custom your field
textField.limitedType = TXLimitedTextFieldTypeCustom;
// you can also set the property in this type. It limits the max number of characters that you input
textField.limitedNumber = 10;
// limitedRegExs is a type of array argument that you can via it, and pass your regular expression to TXLimitedTextField. kTXLimitedTextFieldChineseOnlyRegex is a constant that define in TXMatchConst.h file. it's represent that only Chinese characters can be inputted.
textField.limitedRegExs = @[kTXLimitedTextFieldChineseOnlyRegex];
[self.view addSubview:textField];
使用Nib
InputKit可在属性检查器中访问。这些属性已经可用
关于回调
如果您想要在输入限定的文本时获取回调,必须进行以下操作
-
设置您的TXLimitedTextField的代理
self.limitedTextField.delegate = self;
-
实现
#pragma mark - InputKit - (void)inputKitDidLimitedIllegalInputText:(id)obj { NSLog(@"If you are input text that limited. this method will be callback. you may do some here!"); }
其他
兼容ReactiveCocoa
如果您在项目中使用ReactiveCocoa
,请确保设置compatibleWithRAC
实例属性为YES。(默认为NO)。感谢在GitHub上提出此问题的@devcxm。
示例代码
TXLimitedTextView *limitedTextView = [[TXLimitedTextView alloc] initWithFrame:CGRectMake(20.f, 100.f, 120.f, 30.f)];
// If you are using `ReactiveCocoa` somewhere in your Projects, please make sure this property = YES
limitedTextView.compatibleWithRAC = YES;
limitedTextView.delegate = self;
limitedTextView.limitedNumber = 5;
[self.view addSubview:limitedTextView];
[limitedTextView.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
NSLog(@"come here ... %@", x);
}];
//....Enjoy it!👀
通信
当然可以,如果你愿意,你可以随时为这个项目做出贡献。
-
如果你需要帮助或提出一般问题,请在Weibo或Twitter上@tingxins,当然,你还可以访问我的博客。
-
如果你发现了一个bug,只需打开一个issue。
-
如果你有一个功能请求,只需打开一个issue。
-
如果你想做出贡献,请fork这个仓库,然后提交一个pull request。
许可证
InputKit
可在MIT许可证下使用。请参阅LICENSE文件获取更多信息。
广告
欢迎关注我的微信公众号。