美文网首页
iOS 输入中文自动补英文bug

iOS 输入中文自动补英文bug

作者: 一抹风情 | 来源:发表于2021-04-21 15:14 被阅读0次

iOS 输入中文自动补英文bug

** Bug 原因:**
UITextField、UITextView 使用了 ReactiveObjCReactiveCocoarac_textSignal 方法导致

** 解决办法: **
给UITextField、UITextView 添加分类代替 ReactiveObjCReactiveCocoarac_textSignal 方法:
** UITextField 分类:**

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

- (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

** UITextView 分类:**

#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

希望对您有帮助!
如有帮助,劳烦您随手一赞,谢谢!

相关文章

网友评论

      本文标题:iOS 输入中文自动补英文bug

      本文链接:https://www.haomeiwen.com/subject/pckzlltx.html