Skip to content

Commit a1db2dc

Browse files
committed
refactor(ios): rename textAlignVertical bridge and add Auto fast-path
Rename the ObjC property to `textAlignVertical` so the bridge matches the JS prop and the C++ paragraph attribute exactly. The enum type stays `RCTUITextViewTextAlignmentVertical` since it mirrors the C++ enum class name. Skip the contentSize read for the default Auto / Top case so the new code adds zero per-layout work for apps that don't opt in.
1 parent 1e41147 commit a1db2dc

3 files changed

Lines changed: 24 additions & 21 deletions

File tree

packages/react-native/Libraries/Text/TextInput/Multiline/RCTUITextView.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ typedef NS_ENUM(NSInteger, RCTUITextViewTextAlignmentVertical) {
5555
* multiline value can sit centered or pushed to the bottom of a tall fixed
5656
* frame. Defaults to `Auto` (top-aligned, current behavior).
5757
*/
58-
@property (nonatomic, assign) RCTUITextViewTextAlignmentVertical textAlignmentVertical;
58+
@property (nonatomic, assign) RCTUITextViewTextAlignmentVertical textAlignVertical;
5959

6060
@property (nonatomic, strong, nullable) NSString *inputAccessoryViewID;
6161
@property (nonatomic, strong, nullable) NSString *inputAccessoryViewButtonLabel;

packages/react-native/Libraries/Text/TextInput/Multiline/RCTUITextView.mm

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,12 @@ - (void)textDidChange
150150
[self _invalidatePlaceholderVisibility];
151151
}
152152

153-
- (void)setTextAlignmentVertical:(RCTUITextViewTextAlignmentVertical)textAlignmentVertical
153+
- (void)setTextAlignVertical:(RCTUITextViewTextAlignmentVertical)textAlignVertical
154154
{
155-
if (_textAlignmentVertical == textAlignmentVertical) {
155+
if (_textAlignVertical == textAlignVertical) {
156156
return;
157157
}
158-
_textAlignmentVertical = textAlignmentVertical;
158+
_textAlignVertical = textAlignVertical;
159159
[self setNeedsLayout];
160160
}
161161

@@ -306,25 +306,28 @@ - (void)layoutSubviews
306306
// by RN's existing wiring (only `textContainerInset` is set externally).
307307
- (void)_applyVerticalAlignmentInset
308308
{
309+
// Fast path for apps that don't use the feature: skip the contentSize read.
310+
if (_textAlignVertical == RCTUITextViewTextAlignmentVerticalAuto ||
311+
_textAlignVertical == RCTUITextViewTextAlignmentVerticalTop) {
312+
if (self.contentInset.top != 0) {
313+
UIEdgeInsets contentInset = self.contentInset;
314+
contentInset.top = 0;
315+
self.contentInset = contentInset;
316+
}
317+
return;
318+
}
309319
CGFloat boundsHeight = CGRectGetHeight(self.bounds);
310320
CGFloat contentHeight = self.contentSize.height;
311321
CGFloat topInset = 0;
312322
if (boundsHeight > contentHeight) {
313-
switch (_textAlignmentVertical) {
314-
case RCTUITextViewTextAlignmentVerticalCenter:
315-
topInset = (boundsHeight - contentHeight) / 2;
316-
break;
317-
case RCTUITextViewTextAlignmentVerticalBottom:
318-
topInset = boundsHeight - contentHeight;
319-
break;
320-
case RCTUITextViewTextAlignmentVerticalAuto:
321-
case RCTUITextViewTextAlignmentVerticalTop:
322-
topInset = 0;
323-
break;
323+
if (_textAlignVertical == RCTUITextViewTextAlignmentVerticalCenter) {
324+
topInset = (boundsHeight - contentHeight) / 2;
325+
} else if (_textAlignVertical == RCTUITextViewTextAlignmentVerticalBottom) {
326+
topInset = boundsHeight - contentHeight;
324327
}
325328
}
326-
UIEdgeInsets contentInset = self.contentInset;
327-
if (contentInset.top != topInset) {
329+
if (self.contentInset.top != topInset) {
330+
UIEdgeInsets contentInset = self.contentInset;
328331
contentInset.top = topInset;
329332
self.contentInset = contentInset;
330333
}

packages/react-native/React/Fabric/Mounting/ComponentViews/TextInput/RCTTextInputComponentView.mm

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ - (void)updateProps:(const Props::Shared &)props oldProps:(const Props::Shared &
200200
if (newTextInputProps.multiline &&
201201
newTextInputProps.paragraphAttributes.textAlignVertical !=
202202
oldTextInputProps.paragraphAttributes.textAlignVertical) {
203-
[self _applyTextAlignmentVerticalToMultilineView:newTextInputProps.paragraphAttributes.textAlignVertical];
203+
[self _applyTextAlignVerticalToMultilineView:newTextInputProps.paragraphAttributes.textAlignVertical];
204204
}
205205

206206
if (newTextInputProps.traits.autocapitalizationType != oldTextInputProps.traits.autocapitalizationType) {
@@ -838,14 +838,14 @@ - (void)_setMultiline:(BOOL)multiline
838838

839839
if (multiline) {
840840
const auto &textInputProps = static_cast<const TextInputProps &>(*_props);
841-
[self _applyTextAlignmentVerticalToMultilineView:textInputProps.paragraphAttributes.textAlignVertical];
841+
[self _applyTextAlignVerticalToMultilineView:textInputProps.paragraphAttributes.textAlignVertical];
842842
}
843843
}
844844

845845
// Single-line `UITextField` centers its content vertically inside the field
846846
// frame natively, so this routing is only meaningful when the backed view is
847847
// the multiline `RCTUITextView`. UITextField callers no-op.
848-
- (void)_applyTextAlignmentVerticalToMultilineView:
848+
- (void)_applyTextAlignVerticalToMultilineView:
849849
(const std::optional<facebook::react::TextAlignmentVertical> &)textAlignVertical
850850
{
851851
if (![_backedTextInputView isKindOfClass:[RCTUITextView class]]) {
@@ -868,7 +868,7 @@ - (void)_applyTextAlignmentVerticalToMultilineView:
868868
break;
869869
}
870870
}
871-
((RCTUITextView *)_backedTextInputView).textAlignmentVertical = mapped;
871+
((RCTUITextView *)_backedTextInputView).textAlignVertical = mapped;
872872
}
873873

874874
- (void)_setShowSoftInputOnFocus:(BOOL)showSoftInputOnFocus

0 commit comments

Comments
 (0)