Code Monkey home page Code Monkey logo

inputkit's Introduction

InputKit is an Elegant Kit to limits your input text, inspired by BlocksKit, written in both Objective-C & Swift.

中文介绍

Language

Installation

There are three ways to use InputKit in your project:

  • Using CocoaPods

  • Manual

  • Using Carthage

CocoaPods

CocoaPods is a dependency manager for Objective-C, which automates and simplifies the process of using 3rd-party libraries in your projects.

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'

Manual

Download repo's zip, and just drag ALL files in the InputKit folder to your projects.

Carthage

Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.

You can install Carthage with Homebrew using the following command:

$ brew update
$ brew install carthage

To integrate InputKit or InputKitSwift into your Xcode project using Carthage, specify it in your Cartfile:

github "tingxins/InputKit"

Run carthage to build the frameworks and drag the appropriate framework (InputKit.framework or InputKitSwift.framework) into your Xcode project according to your need. Make sure to add only one framework and not both.

Usage

You can running InputKitDemo for more details.

Using Code

First, import header file when you are using.

#import "InputKit.h"

For UITextField:

Using TXLimitedTextField class instead.

TXLimitedTextFieldTypeDefault:

The type of TXLimitedTextFieldTypeDefault is just limits the number of characters for TXLimitedTextField that you input, and you can input any characters if you wanted, such as Chinese, Alphabet or Special Characters, e.g.

Example:


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];

inputKit-demo-default

TXLimitedTextFieldTypePrice:

The type of TXLimitedTextFieldTypePrice is not only limits the number of characters that you input, and can do more useful limited for your textfield. we usually used in TXLimitedTextField that only need to input int or decimal number.

Example:


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];

inputKit-demo-price

TXLimitedTextFieldTypeCustom:

This type of TXLimitedTextFieldTypeCustom is interesting. you can custom your own TXLimitedTextField, just using your regular expression. but some notes must be noticed when you using.

Example:


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-demo-custom

Using Nib

InputKit accessible from the Attributes Inspector. These attributes have been available:

inputKit-demo-inspector-en

About Callback

If you want to get a callback when you are input limited text, you should have to do this:

  1. Set the delegate of your TXLimitedTextField:

    
    self.limitedTextField.delegate = self;
    
    
  2. Implement -inputKitDidLimitedIllegalInputText: method:

    #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!");
    }
    
    

Other

Compatible with ReactiveCocoa

If you are using ReactiveCocoa in your Projects, please do make sure to set instance property of compatibleWithRAC = YES.(default is NO). Thanks for @devcxm open this issue in GitHub.

Sample Code:


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!👀

Communication

Absolutely,you can contribute to this project all the time if you want to.

  • If you need help or ask general question, just @tingxins in Weibo or Twitter, ofcourse, you can access to my blog.

  • If you found a bug, just open an issue.

  • If you have a feature request, just open an issue.

  • If you want to contribute, fork this repository, and then submit a pull request.

License

InputKit is available under the MIT license. See the LICENSE file for more info.

Ad

Welcome to my Official Account of WeChat.

wechat-qrcode

inputkit's People

Contributors

sergeypetrachkov avatar tingxins avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

inputkit's Issues

LimitedTextField.swift 不设置delegate 不能正常使用

Version: InputKit.swift-1.1.10
目前使用的是Swift3.x
在 Swfit3.x 版本的 InputKit-1.1.10 release 版本的 SwfitDemo里面,如果不设置

textField.delegate = self

的话,无法进行输入限制,OC版是可以不设置delegate的

Simultaneous accesses

Thread 1: Simultaneous accesses to 0x7ff41c812b18, but modification requires exclusive access

set {
limitedDelegate = LimitedTextViewDelegate(realDelegate: newValue)
super.delegate = limitedDelegate
}

使用作者在readme中的例子,只允许中文输入,结果输入不了任何字符

  • (void)viewDidLoad {
    [super viewDidLoad];

    TXLimitedTextField *textField = [[TXLimitedTextField alloc] initWithFrame:CGRectMake(20, 200, 100, 30)];
    textField.backgroundColor = [UIColor blueColor];
    // 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];

    // Do any additional setup after loading the view, typically from a nib.
    }

Does not archive after update to swift4.1 and latest xcode and sdk

❌ /Users/sergeypetrachkov/Money.Cashier.iOS/src/Pods/InputKitSwift/InputKitSwift/LimitedTextView.swift:226:33: value of optional type '((UITextView) -> Bool)?' not unwrapped; did you mean to use '!' or '?'?

flag = realDelegate.textViewShouldEndEditing(textView)
^

❌ /Users/sergeypetrachkov/Money.Cashier.iOS/src/Pods/InputKitSwift/InputKitSwift/LimitedTextView.swift:234:26: value of optional type '((UITextView) -> Void)?' not unwrapped; did you mean to use '!' or '?'?

realDelegate.textViewDidBeginEditing(textView)
^

❌ /Users/sergeypetrachkov/Money.Cashier.iOS/src/Pods/InputKitSwift/InputKitSwift/LimitedTextView.swift:241:26: value of optional type '((UITextView) -> Void)?' not unwrapped; did you mean to use '!' or '?'?

realDelegate.textViewDidEndEditing(textView)
^

❌ /Users/sergeypetrachkov/Money.Cashier.iOS/src/Pods/InputKitSwift/InputKitSwift/LimitedTextView.swift:250:33: value of optional type '((UITextView, NSRange, String) -> Bool)?' (aka 'Optional<(UITextView, _NSRange, String) -> Bool>') not unwrapped; did you mean to use '!' or '?'?

flag = realDelegate.textView(textView, shouldChangeTextIn: range, replacementText: text)
^

❌ /Users/sergeypetrachkov/Money.Cashier.iOS/src/Pods/InputKitSwift/InputKitSwift/LimitedTextView.swift:285:26: value of optional type '((UITextView) -> Void)?' not unwrapped; did you mean to use '!' or '?'?

realDelegate.textViewDidChange(textView)
^

❌ /Users/sergeypetrachkov/Money.Cashier.iOS/src/Pods/InputKitSwift/InputKitSwift/LimitedTextView.swift:292:26: value of optional type '((UITextView) -> Void)?' not unwrapped; did you mean to use '!' or '?'?

realDelegate.textViewDidChangeSelection(textView)
^

❌ /Users/sergeypetrachkov/Money.Cashier.iOS/src/Pods/InputKitSwift/InputKitSwift/LimitedTextView.swift:300:33: value of optional type '((UITextView, URL, NSRange, UITextItemInteraction) -> Bool)?' (aka 'Optional<(UITextView, URL, _NSRange, UITextItemInteraction) -> Bool>') not unwrapped; did you mean to use '!' or '?'?

flag = realDelegate.textView(textView, shouldInteractWith: URL, in: characterRange, interaction: interaction)
^

❌ /Users/sergeypetrachkov/Money.Cashier.iOS/src/Pods/InputKitSwift/InputKitSwift/LimitedTextView.swift:310:33: value of optional type '((UITextView, NSTextAttachment, NSRange, UITextItemInteraction) -> Bool)?' (aka 'Optional<(UITextView, NSTextAttachment, _NSRange, UITextItemInteraction) -> Bool>') not unwrapped; did you mean to use '!' or '?'?

flag = realDelegate.textView(textView, shouldInteractWith: textAttachment, in: characterRange, interaction: interaction)
^

❌ /Users/sergeypetrachkov/Money.Cashier.iOS/src/Pods/InputKitSwift/InputKitSwift/LimitedTextView.swift:319:33: value of optional type '((UITextView, URL, NSRange) -> Bool)?' (aka 'Optional<(UITextView, URL, _NSRange) -> Bool>') not unwrapped; did you mean to use '!' or '?'?

flag = realDelegate.textView(textView, shouldInteractWith: URL, in: characterRange)
^

❌ /Users/sergeypetrachkov/Money.Cashier.iOS/src/Pods/InputKitSwift/InputKitSwift/LimitedTextView.swift:328:33: value of optional type '((UITextView, NSTextAttachment, NSRange) -> Bool)?' (aka 'Optional<(UITextView, NSTextAttachment, _NSRange) -> Bool>') not unwrapped; did you mean to use '!' or '?'?

etc

兼容textFieldDidEndEditing

根据苹果API注释

- (void)textFieldDidEndEditing:(UITextField *)textField reason:(UITextFieldDidEndEditingReason)reason NS_AVAILABLE_IOS(10_0); 
// if implemented, called in place of textFieldDidEndEditing:

建议作以下修改,这样才能遵循苹果原来的代理调用顺序,否则真正代理只实现了textFieldDidEndEditing的时候并不能被调用。

- (void)textFieldDidEndEditing:(UITextField *)textField reason:(UITextFieldDidEndEditingReason)reason NS_AVAILABLE_IOS(10_0) {
    id realDelegate = self.realDelegate;
    if (realDelegate) {
        if ([realDelegate respondsToSelector:@selector(textFieldDidEndEditing:reason:)])
            [realDelegate textFieldDidEndEditing:textField reason:reason];
        else if ([realDelegate respondsToSelector:@selector(textFieldDidEndEditing:)])
            [realDelegate textFieldDidEndEditing:textField];
    }
}

真正的代理里把text=nil出问题

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;

假如在真正的代理里执行以上方法时把textView.text=nil,再执行以下方法进行匹配就会出错

    BOOL flag = YES;
    id realDelegate = self.realDelegate;
    if (realDelegate && [realDelegate respondsToSelector:@selector(textView:shouldChangeTextInRange:replacementText:)])
        flag = [realDelegate textView:textView shouldChangeTextInRange:range replacementText:text];
    
    int matchResult = YES;
    
    if ([textView isKindOfClass:[TXLimitedTextView class]]) {
        TXLimitedTextView *limitedTextView = (TXLimitedTextView *)textView;
        
        NSMutableString *matchStr = [NSMutableString stringWithString:textView.text];

所以不管TXLimitedTextView还是TXLimitedTextField,在匹配正则时应该判断.text是否为空。

Typo in LimitedTextField.swift breaks control behavior

Return button on keyboard does not work because we check for wrong selector textFieldShouldClear in textFieldShouldReturn

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        var flag = true
        if let realDelegate = self.realDelegate, realDelegate.responds(to: #selector(textFieldShouldClear(_:))) {
            flag = realDelegate.textFieldShouldClear(textField)
        }
        return flag
    }

身份证正则 问题

let kLimitedTextCarIDRegex = "(^[0-9]{15}$)|([0-9]{17}([0-9]|X)$)"
textField.limitedRegEx = MatchConstant.Name.kLimitedTextCarIDRegex
textField.limitedType = .custom
输入框什么都不能输入,这种情况应该咋用啊?

正则限制,输入中文报错

image
image

2017-06-28 16:55:36.990 InputKitDemo_OC[22341:244100] *** Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFString replaceOccurrencesOfString:withString:options:range:]: Range {9, 10} out of bounds; string length 10'
*** First throw call stack:
(
	0   CoreFoundation                      0x0000000108052d4b __exceptionPreprocess + 171
	1   libobjc.A.dylib                     0x0000000107ab421e objc_exception_throw + 48
	2   CoreFoundation                      0x00000001080bc2b5 +[NSException raise:format:] + 197
	3   CoreFoundation                      0x0000000107fb8e93 -[__NSCFString replaceOccurrencesOfString:withString:options:range:] + 531
	4   Foundation                          0x00000001075992a0 -[NSString stringByReplacingOccurrencesOfString:withString:options:range:] + 184
	5   InputKitDemo_OC                     0x00000001074c1da1 -[TXLimitedTextField textFieldTextDidChangeNotification:] + 1649
	6   CoreFoundation                      0x0000000107ff05ec __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 12
	7   CoreFoundation                      0x0000000107ff04eb _CFXRegistrationPost + 427
	8   CoreFoundation                      0x0000000107ff0252 ___CFXNotificationPost_block_invoke + 50
	9   CoreFoundation                      0x0000000107fb3282 -[_CFXNotificationRegistrar find:object:observer:enumerator:] + 2018
	10  CoreFoundation                      0x0000000107fb231b _CFXNotificationPost + 667
	11  Foundation                          0x000000010757a81b -[NSNotificationCenter postNotificationName:object:userInfo:] + 66
	12  UIKit                               0x0000000108f976d4 -[UITextField fieldEditorDidChange:] + 257
	13  UIKit                               0x0000000108fa5a53 -[UITextInputController _sendDelegateChangeNotificationsForText:selection:] + 120
	14  UIKit                               0x0000000108f9b7a5 -[UITextField unmarkText] + 48
	15  UIKit                               0x0000000108a8aa51 -[UIResponder(UITextInput_Internal) _unmarkText] + 61
	16  UIKit                               0x000000010873f8dd -[UIKeyboardImpl unmarkText] + 43
	17  UIKit                               0x000000010873f9df -[UIKeyboardImpl unmarkText:] + 180
	18  UIKit                               0x0000000108742a6d __59-[UIKeyboardImpl handleAcceptedCandidate:executionContext:]_block_invoke_2 + 180
	19  UIKit                               0x0000000108fc736b -[UIKeyboardTaskQueue continueExecutionOnMainThread] + 385
	20  UIKit                               0x0000000108fc7e70 -[UIKeyboardTaskQueue waitUntilTaskIsFinished:] + 261
	21  UIKit                               0x0000000108fc7fd3 -[UIKeyboardTaskQueue performSingleTask:] + 268
	22  UIKit                               0x000000010874b869 -[UIKeyboardImpl acceptCurrentCandidateForInput:] + 249
	23  UIKit                               0x0000000108b7bca2 -[UIKeyboardCandidateBar collectionView:didSelectItemAtIndexPath:] + 246
	24  UIKit                               0x0000000108daf6e4 -[UICollectionView _selectItemAtIndexPath:animated:scrollPosition:notifyDelegate:] + 702
	25  UIKit                               0x0000000108dda4d2 -[UICollectionView touchesEnded:withEvent:] + 649
	26  UIKit                               0x000000010868df6b forwardTouchMethod + 348
	27  UIKit                               0x000000010868e034 -[UIResponder touchesEnded:withEvent:] + 49
	28  UIKit                               0x000000010868df6b forwardTouchMethod + 348
	29  UIKit                               0x000000010868e034 -[UIResponder touchesEnded:withEvent:] + 49
	30  UIKit                               0x00000001084e6545 -[UIWindow _sendTouchesForEvent:] + 2747
	31  UIKit                               0x00000001084e7c33 -[UIWindow sendEvent:] + 4011
	32  UIKit                               0x00000001084949ab -[UIApplication sendEvent:] + 371
	33  UIKit                               0x0000000108c8172d __dispatchPreprocessedEventFromEventQueue + 3248
	34  UIKit                               0x0000000108c7a463 __handleEventQueue + 4879
	35  CoreFoundation                      0x0000000107ff7761 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
	36  CoreFoundation                      0x0000000107fdc98c __CFRunLoopDoSources0 + 556
	37  CoreFoundation                      0x0000000107fdbe76 __CFRunLoopRun + 918
	38  CoreFoundation                      0x0000000107fdb884 CFRunLoopRunSpecific + 420
	39  GraphicsServices                    0x000000010be44a6f GSEventRunModal + 161
	40  UIKit                               0x0000000108476c68 UIApplicationMain + 159
	41  InputKitDemo_OC                     0x00000001074c713f main + 111
	42  libdyld.dylib                       0x000000010aeb468d start + 1
	43  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.