react-native 0.55.4 TextInput在iOS上不能修改value值問題

參考了0.57的源碼,但是發(fā)現(xiàn)如果value初始設(shè)置為空字符時(shí)整個(gè)輸入框就不能輸入了,所以在這個(gè)基礎(chǔ)上我調(diào)整了一下代碼,解決了這個(gè)問題,+開頭的就是新增代碼,暫時(shí)運(yùn)行穩(wěn)定,如果有疑問可以留言聯(lián)系我。

//RCTBaseTextInputShadowView.m

@implementation RCTBaseTextInputShadowView
{
  __weak RCTBridge *_bridge;
  NSAttributedString *_Nullable _previousAttributedText;
  BOOL _needsUpdateView;
  NSAttributedString *_Nullable _localAttributedText;
  CGSize _previousContentSize;

  NSTextStorage *_textStorage;
  NSTextContainer *_textContainer;
  NSLayoutManager *_layoutManager;
+  NSString *_text;
+  NSString *_haveDefaultText;
}

+- (NSString *)text
+{
+  return _text;
+}
+- (void)setText:(NSString *)text
+{
+  if (!_haveDefaultText) {
+    if (text.length) {
+      _haveDefaultText = @"1";
+    } else {
+      _haveDefaultText = @"0";
+    }
+  }
+  NSString *textTemp = _text;
+  _text = text;
+  
+  // Clear `_previousAttributedText` to notify the view about the change
+  // when `text` native prop is set.
+  NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithAttributedString:[self attributedTextWithBaseTextAttributes:nil]];
+  
+  [attributedText removeAttribute:RCTBaseTextShadowViewEmbeddedShadowViewAttributeName
                            range:NSMakeRange(0, attributedText.length)];
+  
+  [attributedText removeAttribute:RCTTextAttributesTagAttributeName
                            range:NSMakeRange(0, attributedText.length)];
+  if ((_haveDefaultText && text && text.length==0) || ([_haveDefaultText isEqualToString:@"1"] && textTemp.length)) {
+    NSAttributedString *propertyAttributedText =
+    [[NSAttributedString alloc] initWithString:textTemp?textTemp:@""
                                    attributes:self.textAttributes.effectiveTextAttributes];
+    [attributedText insertAttributedString:propertyAttributedText atIndex:0];
+  }
+  
+  _previousAttributedText = [attributedText copy];
+  
+  [self dirtyLayout];
+}

參考:https://github.com/facebook/react-native/issues/18874

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容