參考了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];
+}