Skip to content

Add unit tests for natural text alignment on iOS - #59004

Closed
YevheniiKotyrlo wants to merge 1 commit into
react:mainfrom
YevheniiKotyrlo:fix/ios-natural-alignment-ltr
Closed

YevheniiKotyrlo wants to merge 1 commit into
react:mainfrom
YevheniiKotyrlo:fix/ios-natural-alignment-ltr

Conversation

@YevheniiKotyrlo

@YevheniiKotyrlo YevheniiKotyrlo commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

Summary:

Adds tests for the alignment RCTNSTextAttributesFromTextAttributes gives a paragraph that declares no textAlign. #57634 and #59249 resolve it to the start edge of the paragraph's layout direction, rather than leaving NSTextAlignmentNatural, which UIKit resolves against the app's localization, and neither added a test:

  • testUndeclaredTextAlignmentResolvesWithLayoutDirection: a left-to-right paragraph is aligned left and a right-to-left one right. It asserts that the paragraph style exists, because a missing one reads as NSTextAlignmentLeft, so a check of the alignment alone would pass without [iOS] Resolve natural text alignment from the layout direction for LTR as well as RTL #59249.
  • testUndeclaredTextAlignmentWithoutLayoutDirectionStaysNatural: text attributes with no layout direction, as a text input's, get no paragraph style, so a UITextField keeps aligning to its own view.

Changelog:

[Internal]

Test Plan:

  • RCTAttributedTextUtilsTest on main at 19d6d17 with this change: 26 tests, 0 failures, on an iPhone 12 Pro Max (iOS 27.0.1, Xcode 27.1). The file belongs to React-RCTFabric's Tests test spec, which no Podfile enables, so for the run I added it to RNTesterUnitTests, hosted in RNTester.
  • testUndeclaredTextAlignmentResolvesWithLayoutDirection fails on main before [iOS] Resolve natural text alignment from the layout direction for LTR as well as RTL #59249 (1c6e0c7) at XCTAssertNotNil(leftToRight): React-RCTFabric-Unit-Tests on an iOS 18.0 simulator, 56 tests with 1 failure.
  • yarn format-cpp leaves the file unchanged.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Oct 12, 2026
@YevheniiKotyrlo

Copy link
Copy Markdown
Contributor Author

Device evidence — before / after

UNFIXED — under a right-to-left app language, the two auto (default) lines in this direction: 'ltr' box are drawn flush right.

FIXED — they are drawn flush left, the start of their layout direction; the declared left, center, right and justify lines do not move.

before — main at 1c6e0c7738 after — with this PR

iPhone 16 simulator, iOS 18.0, RNTester › Text › Text Align with RTL in its initial LTR state, launched with -AppleTextDirection YES -NSForceRightToLeftWritingDirection YES, one session. Only the rows of the two auto (default) lines differ: the English line's ink spans x 653–1176 before and 2–524 after, the Arabic line's 370–1176 and 2–808, of a 1179-px row.

@YevheniiKotyrlo
YevheniiKotyrlo marked this pull request as ready for review October 12, 2026 05:05
@facebook-github-tools facebook-github-tools Bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Oct 12, 2026
BOOL isParagraphStyleUsed = NO;
const bool isRTL = textAttributes.layoutDirection == LayoutDirection::RightToLeft;
if (textAttributes.alignment.has_value() || isRTL) {
if (textAttributes.alignment.has_value() || textAttributes.layoutDirection.has_value()) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The line above (const bool isRTL = textAttributes.layoutDirection == LayoutDirection::RightToLeft;) could be moved inside the condition.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#59249 landed the same change on main in a340eec, with isRTL inside the condition, so after updating to main this PR only adds the tests (24bc565).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can close this PR then.

@YevheniiKotyrlo
YevheniiKotyrlo marked this pull request as draft October 26, 2026 20:21
RCTNSTextAttributesFromTextAttributes resolves a paragraph that declares
no textAlign to the start edge of its layout direction, left to right as
well as right to left (react#57634, react#59249), and leaves it to UIKit when the
text attributes carry no layout direction, as a text input's do.

Cover the three cases. The left-to-right one fails without react#59249, where
the paragraph gets no paragraph style at all: a test that read only the
alignment would pass there, since a nil paragraph style reads as
NSTextAlignmentLeft.
@YevheniiKotyrlo
YevheniiKotyrlo force-pushed the fix/ios-natural-alignment-ltr branch from 00fef63 to 24bc565 Compare October 26, 2026 21:42
@YevheniiKotyrlo YevheniiKotyrlo changed the title Align text to the left by default in LTR on iOS Add unit tests for natural text alignment on iOS Oct 26, 2026
@YevheniiKotyrlo

Copy link
Copy Markdown
Contributor Author

Closing in favor of #59249, which landed this change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants