rac_textSignal自带键盘输入中文的问题

需求:

限制输入字符50个及以内

问题:

rac_textSignal在预输入拼音的时候会响应,导致限制出错

解决方案:

过滤预输入状态。(感谢ShowMeCode的这篇文章)

#import "UITextField+Add.h"
#import "NSObject+RACDescription.h"

@implementation UITextField (Add)

- (RACSignal *)rac_inputTextSignal {
    @weakify(self);
    return [[[[[[RACSignal
                defer:^{
                    @strongify(self);
                    return [RACSignal return:self];
                }]
               concat:[self rac_signalForControlEvents:UIControlEventAllEditingEvents]]
              filter:^BOOL(UITextField *x) {
                  if (!x.markedTextRange) {
                      return YES;
                  } else {
                      return NO;
                  }
              }]
              map:^(UITextField *x) {
                  return x.text;
              }]
             takeUntil:self.rac_willDeallocSignal]
            setNameWithFormat:@"%@ -rac_inputTextSignal", RACDescription(self)];
}

@end

#import "UITextView+Add.h"
#import "NSObject+RACDescription.h"


@implementation UITextView (Add)

static void RACUseDelegateProxy_(UITextView *self) {
    if (self.delegate == (id)self.rac_delegateProxy) return;
    
    self.rac_delegateProxy.rac_proxiedDelegate = self.delegate;
    self.delegate = (id)self.rac_delegateProxy;
}

- (RACSignal *)rac_inputTextSignal {
    @weakify(self);
    RACSignal *signal = [[[[[[[RACSignal
                             defer:^{
                                 @strongify(self);
                                 return [RACSignal return:RACTuplePack(self)];
                             }]
                            concat:[self.rac_delegateProxy signalForSelector:@selector(textViewDidChange:)]]
                            reduceEach:^(UITextView *x) {
                                return x;
                            }]
                           filter:^BOOL(UITextView *x) {
                               if (!x.markedTextRange) {
                                   return YES;
                               } else {
                                   return NO;
                               }
                           }]
                           map:^(UITextView *x) {
                               return x.text;
                           }]
                          takeUntil:self.rac_willDeallocSignal]
                         setNameWithFormat:@"%@ -rac_inputTextSignal", RACDescription(self)];
    
    RACUseDelegateProxy_(self);
    
    return signal;
}

@end

你可能感兴趣的:(rac_textSignal自带键盘输入中文的问题)