InputKit 是一个优雅的文本输入限制套件,灵感来自 BlocksKit,用 Objective-C 和 Swift 编写。
有三种方法将 InputKit 用于您的项目
使用 CocoaPods
手动
使用 Carthage
CocoaPods 是 Objective-C 的依赖管理器,它自动化并简化了在项目中使用第三方库的过程。
Podfile
platform :ios, '8.0'
pod 'InputKit', 'xxx'
下载仓库的 zip 文件,然后将 InputKit 文件夹中的所有文件拖放到您的项目中。
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"
使用 TXLimitedTextField 类代替。
TXLimitedTextFieldTypeDefault
TXLimitedTextFieldTypeDefault 的类型仅限制输入到 TXLimitedTextField 中的字符数量,并且如果您想的话可以输入任何字符,例如中文、字母或特殊字符,例如:
示例
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];
InputKit 可在 属性检查器 中访问。这些属性已经可用
如果您想在输入限制文本时获取回调,则必须这样做
设置您的 TXLimitedTextField 的委托
self.limitedTextField.delegate = self;
实现 -inputKitDidLimitedIllegalInputText: 方法
#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
,请确保将 compatibleWithRAC
实例属性的值设置为 YES。(默认值为 NO)。感谢 @devcxm 在 GitHub 上的问题。
示例代码
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!👀
当然,如果您愿意,您可以随时为此项目做出贡献。
如果您发现了一个bug,请打开一个issue。
如果您有功能请求,请打开一个issue。
如果您想贡献,请克隆此仓库,然后提交一个pull request。
InputKit在MIT许可下可用。更多信息请参阅LICENSE文件。
欢迎关注我的微信公众号。