参考了0.57的源码,但是发现如果value初始设置为空字符时整个输入框就不能输入了,所以在这个基础上我调整了一下代码,解决了这个问题,+开头的就是新增代码,暂时运行稳定,如果有疑问可以留言联系我。
//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];
+}
网友评论