Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ @implementation RCTTextInputComponentView {

BOOL _hasInputAccessoryView;
CGSize _previousContentSize;

/*
* The input's paragraph style, with a wrapping line break mode replaced, when the field draws without its line
* height, or nil. TextKit puts a line's extra leading above the glyphs, where CSS splits it equally above and below
* them (https://www.w3.org/TR/CSS2/visudet.html#leading). A single-line field gets the CSS placement by drawing
* without the line height, because `UITextField` centers a line of the font's own height in the field.
*/
NSParagraphStyle *_omittedParagraphStyle;
}

#pragma mark - UIView overrides
Expand Down Expand Up @@ -156,9 +164,7 @@ - (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection

if (UITraitCollection.currentTraitCollection.preferredContentSizeCategory !=
previousTraitCollection.preferredContentSizeCategory) {
const auto &newTextInputProps = static_cast<const TextInputProps &>(*_props);
_backedTextInputView.defaultTextAttributes =
RCTNSTextAttributesFromTextAttributes(newTextInputProps.getEffectiveTextAttributes(RCTFontSizeMultiplier()));
[self _updateDefaultTextAttributesWithProps:static_cast<const TextInputProps &>(*_props)];
}
}

Expand Down Expand Up @@ -327,12 +333,11 @@ - (void)updateProps:(const Props::Shared &)props oldProps:(const Props::Shared &
_backedTextInputView.placeholderColor = RCTUIColorFromSharedColor(newTextInputProps.placeholderTextColor);
}

if (newTextInputProps.textAttributes != oldTextInputProps.textAttributes) {
NSMutableDictionary<NSAttributedStringKey, id> *defaultAttributes =
RCTNSTextAttributesFromTextAttributes(newTextInputProps.getEffectiveTextAttributes(RCTFontSizeMultiplier()));
defaultAttributes[RCTAttributedStringEventEmitterKey] =
_backedTextInputView.defaultTextAttributes[RCTAttributedStringEventEmitterKey];
_backedTextInputView.defaultTextAttributes = defaultAttributes;
// Recompute the attributes when `multiline` changes too: only a single-line field omits the line height and
// replaces a wrapping line break mode.
if (newTextInputProps.textAttributes != oldTextInputProps.textAttributes ||
newTextInputProps.multiline != oldTextInputProps.multiline) {
[self _updateDefaultTextAttributesWithProps:newTextInputProps];
}

if (newTextInputProps.selectionColor != oldTextInputProps.selectionColor) {
Expand Down Expand Up @@ -820,15 +825,23 @@ - (void)_updateState
if (!_state) {
return;
}
NSAttributedString *attributedString = _backedTextInputView.attributedText;
auto data = _state->getData();
_lastStringStateWasUpdatedWith = attributedString;
data.attributedStringBox = RCTAttributedStringBoxFromNSAttributedString(attributedString);
_lastStringStateWasUpdatedWith = _backedTextInputView.attributedText;
data.attributedStringBox = RCTAttributedStringBoxFromNSAttributedString([self _attributedStringForState]);
_mostRecentEventCount += _comingFromJS ? 0 : 1;
data.mostRecentEventCount = _mostRecentEventCount;
_state->updateState(std::move(data));
}

// Returns the field's text with the paragraph styles the field draws without, which is the form the state holds.
- (NSAttributedString *)_attributedStringForState
{
NSAttributedString *attributedString = _backedTextInputView.attributedText;
return _omittedParagraphStyle != nil
? RCTAttributedStringByRestoringLineHeight(attributedString, _omittedParagraphStyle)
: attributedString;
}

- (AttributedString::Range)_selectionRange
{
UITextRange *selectedTextRange = _backedTextInputView.selectedTextRange;
Expand Down Expand Up @@ -860,19 +873,52 @@ - (void)_restoreTextSelectionAndIgnoreCaretChange:(BOOL)ignore
[_backedTextInputView setSelectedTextRange:range notifyDelegate:YES];
}

// Sets the field's default text attributes from `props`, and `_omittedParagraphStyle` with them.
- (void)_updateDefaultTextAttributesWithProps:(const TextInputProps &)props
{
NSMutableDictionary<NSAttributedStringKey, id> *attributes =
RCTNSTextAttributesFromTextAttributes(props.getEffectiveTextAttributes(RCTFontSizeMultiplier()));
NSParagraphStyle *paragraphStyle = attributes[NSParagraphStyleAttributeName];
if (!props.multiline && paragraphStyle != nil) {
paragraphStyle = RCTSingleLineParagraphStyle(paragraphStyle);
attributes[NSParagraphStyleAttributeName] = paragraphStyle;
}
// Record the style after that replacement: `_setAttributedString` compares the restored text with a state's
// text whose styles are replaced too.
_omittedParagraphStyle = !props.multiline && paragraphStyle.maximumLineHeight > 0 ? [paragraphStyle copy] : nil;
if (_omittedParagraphStyle != nil) {
attributes[NSParagraphStyleAttributeName] = RCTParagraphStyleByOmittingLineHeight(paragraphStyle);
}
attributes[RCTAttributedStringEventEmitterKey] =
_backedTextInputView.defaultTextAttributes[RCTAttributedStringEventEmitterKey];
_backedTextInputView.defaultTextAttributes = attributes;
}

- (void)_setAttributedString:(NSAttributedString *)attributedString
{
if ([self _textOf:attributedString equals:_backedTextInputView.attributedText]) {
BOOL isMultiline = _props ? static_cast<const TextInputProps &>(*_props).multiline
: [_backedTextInputView isKindOfClass:[UITextView class]];
// Replace wrapping paragraph styles before comparing: the field's text has replaced ones, and a state built
// from the React tree does not. The view writes the replaced styles to the state, which is safe because layout
// measures a single-line input at unlimited width, where wrapping and truncating give the same size.
if (!isMultiline) {
attributedString = RCTAttributedStringWithSingleLineParagraphStyles(attributedString);
}
// Compare with the paragraph styles restored: the field draws with other styles than the state's text has, so
// only the restored text can match it.
NSAttributedString *oldAttributedString = [self _attributedStringForState];
if ([self _textOf:attributedString equals:oldAttributedString]) {
return;
}
if (_omittedParagraphStyle != nil) {
attributedString = RCTAttributedStringByOmittingLineHeight(attributedString, _omittedParagraphStyle);
}
UITextRange *selectedRange = _backedTextInputView.selectedTextRange;
NSInteger oldTextLength = _backedTextInputView.attributedText.string.length;
_backedTextInputView.attributedText = attributedString;
// A single-line input that is not being edited has no cursor to keep: UIKit places one when editing begins.
// Restoring it anyway makes UIKit scroll the field editor to it when the text doesn't fit, and the editor keeps
// that offset after the text fits again, so text typed later in this view (also after recycling) is drawn too high.
BOOL isMultiline = _props ? static_cast<const TextInputProps &>(*_props).multiline
: [_backedTextInputView isKindOfClass:[UITextView class]];
if (isMultiline || _backedTextInputView.isFirstResponder) {
// Updating the UITextView attributedText, for example changing the lineHeight, the color or adding
// a new paragraph with \n, causes the cursor to move to the end of the Text and scroll.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,41 @@ void RCTCopyBackedTextInput(
UIView<RCTBackedTextInputViewProtocol> *fromTextInput,
UIView<RCTBackedTextInputViewProtocol> *toTextInput);

/*
* Returns `paragraphStyle` with `NSLineBreakByTruncatingTail` in place of a wrapping line break mode, as
* `UITextField`'s own default paragraph style has. A text field that is not being edited lays out its text as the
* paragraph style says, so a wrapping mode draws a second line.
*/
NSParagraphStyle *RCTSingleLineParagraphStyle(NSParagraphStyle *paragraphStyle);

/*
* Returns `attributedString` with each paragraph style replaced as `RCTSingleLineParagraphStyle` does.
*/
NSAttributedString *RCTAttributedStringWithSingleLineParagraphStyles(NSAttributedString *attributedString);

/*
* Returns `paragraphStyle` with no minimum or maximum line height and the rest of the style unchanged.
*/
NSParagraphStyle *RCTParagraphStyleByOmittingLineHeight(NSParagraphStyle *paragraphStyle);

/*
* Returns `attributedString` with each paragraph style that has a line height replaced as
* `RCTParagraphStyleByOmittingLineHeight` does. A private attribute records the replaced style of each range
* where it is not `inputParagraphStyle`, the input's own, so that the other ranges follow the input when its
* style changes.
*/
NSAttributedString *RCTAttributedStringByOmittingLineHeight(
NSAttributedString *attributedString,
NSParagraphStyle *inputParagraphStyle);

/*
* Reverses `RCTAttributedStringByOmittingLineHeight`: each range gets back the paragraph style recorded for it. A
* range with no record, such as the input's own text and text typed into the field, gets `inputParagraphStyle`.
*/
NSAttributedString *RCTAttributedStringByRestoringLineHeight(
NSAttributedString *attributedString,
NSParagraphStyle *inputParagraphStyle);

UITextAutocorrectionType RCTUITextAutocorrectionTypeFromOptionalBool(std::optional<bool> autoCorrect);

UITextAutocapitalizationType RCTUITextAutocapitalizationTypeFromAutocapitalizationType(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,87 @@ void RCTCopyBackedTextInput(
[toTextInput setSelectedTextRange:fromTextInput.selectedTextRange notifyDelegate:NO];
}

NSParagraphStyle *RCTSingleLineParagraphStyle(NSParagraphStyle *paragraphStyle)
{
if (paragraphStyle.lineBreakMode != NSLineBreakByWordWrapping &&
paragraphStyle.lineBreakMode != NSLineBreakByCharWrapping) {
return paragraphStyle;
}
NSMutableParagraphStyle *copy = [paragraphStyle mutableCopy];
copy.lineBreakMode = NSLineBreakByTruncatingTail;
return copy;
}

NSAttributedString *RCTAttributedStringWithSingleLineParagraphStyles(NSAttributedString *attributedString)
{
NSMutableAttributedString *copy = [attributedString mutableCopy];
[attributedString enumerateAttribute:NSParagraphStyleAttributeName
inRange:NSMakeRange(0, attributedString.length)
options:0
usingBlock:^(NSParagraphStyle *paragraphStyle, NSRange range, BOOL *stop) {
if (paragraphStyle != nil) {
[copy addAttribute:NSParagraphStyleAttributeName
value:RCTSingleLineParagraphStyle(paragraphStyle)
range:range];
}
}];
return copy;
}

static const NSAttributedStringKey RCTOmittedParagraphStyleAttributeName = @"RCTOmittedParagraphStyle";

NSParagraphStyle *RCTParagraphStyleByOmittingLineHeight(NSParagraphStyle *paragraphStyle)
{
NSMutableParagraphStyle *copy = [paragraphStyle mutableCopy];
copy.minimumLineHeight = 0;
copy.maximumLineHeight = 0;
return copy;
}

NSAttributedString *RCTAttributedStringByOmittingLineHeight(
NSAttributedString *attributedString,
NSParagraphStyle *inputParagraphStyle)
{
NSMutableAttributedString *copy = [attributedString mutableCopy];
[attributedString enumerateAttribute:NSParagraphStyleAttributeName
inRange:NSMakeRange(0, attributedString.length)
options:0
usingBlock:^(NSParagraphStyle *paragraphStyle, NSRange range, BOOL *stop) {
// Leave a style with no line height as it is: a record of it would keep the
// input's line height from the range on restore.
if (paragraphStyle.maximumLineHeight <= 0) {
return;
}
[copy addAttribute:NSParagraphStyleAttributeName
value:RCTParagraphStyleByOmittingLineHeight(paragraphStyle)
range:range];
if (![paragraphStyle isEqual:inputParagraphStyle]) {
[copy addAttribute:RCTOmittedParagraphStyleAttributeName
value:paragraphStyle
range:range];
}
}];
return copy;
}

NSAttributedString *RCTAttributedStringByRestoringLineHeight(
NSAttributedString *attributedString,
NSParagraphStyle *inputParagraphStyle)
{
NSRange fullRange = NSMakeRange(0, attributedString.length);
NSMutableAttributedString *copy = [attributedString mutableCopy];
[copy removeAttribute:RCTOmittedParagraphStyleAttributeName range:fullRange];
[attributedString enumerateAttribute:RCTOmittedParagraphStyleAttributeName
inRange:fullRange
options:0
usingBlock:^(NSParagraphStyle *omittedParagraphStyle, NSRange range, BOOL *stop) {
[copy addAttribute:NSParagraphStyleAttributeName
value:omittedParagraphStyle ?: inputParagraphStyle
range:range];
}];
return copy;
}

UITextAutocorrectionType RCTUITextAutocorrectionTypeFromOptionalBool(std::optional<bool> autoCorrect)
{
return autoCorrect.has_value() ? (*autoCorrect ? UITextAutocorrectionTypeYes : UITextAutocorrectionTypeNo)
Expand Down
Loading
Loading