Skip to content

Commit 24bc565

Browse files
Add unit tests for natural text alignment on iOS
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 (#57634, #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 #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.
1 parent 19d6d17 commit 24bc565

1 file changed

Lines changed: 25 additions & 0 deletions

File tree

‎packages/react-native/React/Tests/Text/RCTAttributedTextUtilsTest.mm‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,15 @@ static NSTextAlignment NSTextAlignmentFromTextAlign(NSString *textAlign, LayoutD
4343
return paragraphStyle.alignment;
4444
}
4545

46+
static NSParagraphStyle *ParagraphStyleFromLayoutDirection(std::optional<LayoutDirection> layoutDirection)
47+
{
48+
TextAttributes textAttributes;
49+
textAttributes.layoutDirection = layoutDirection;
50+
51+
NSDictionary<NSAttributedStringKey, id> *attributes = RCTNSTextAttributesFromTextAttributes(textAttributes);
52+
return attributes[NSParagraphStyleAttributeName];
53+
}
54+
4655
static UIFont *FontFromSettings(std::optional<std::string> settings, std::optional<FontWeight> weight = std::nullopt)
4756
{
4857
TextAttributes textAttributes;
@@ -133,6 +142,22 @@ - (void)testTextAlignmentStartAndEndResolveWithLayoutDirection
133142
XCTAssertEqual(NSTextAlignmentFromTextAlign(@"end", LayoutDirection::RightToLeft), NSTextAlignmentLeft);
134143
}
135144

145+
- (void)testUndeclaredTextAlignmentResolvesWithLayoutDirection
146+
{
147+
NSParagraphStyle *leftToRight = ParagraphStyleFromLayoutDirection(LayoutDirection::LeftToRight);
148+
XCTAssertNotNil(leftToRight);
149+
XCTAssertEqual(leftToRight.alignment, NSTextAlignmentLeft);
150+
151+
NSParagraphStyle *rightToLeft = ParagraphStyleFromLayoutDirection(LayoutDirection::RightToLeft);
152+
XCTAssertNotNil(rightToLeft);
153+
XCTAssertEqual(rightToLeft.alignment, NSTextAlignmentRight);
154+
}
155+
156+
- (void)testUndeclaredTextAlignmentWithoutLayoutDirectionStaysNatural
157+
{
158+
XCTAssertNil(ParagraphStyleFromLayoutDirection(std::nullopt));
159+
}
160+
136161
- (void)testSamenessOfEmptyAttributedStrings
137162
{
138163
NSAttributedString *attributedString1 = [[NSAttributedString alloc] initWithString:@""];

0 commit comments

Comments
 (0)