Skip to content

Commit a39660f

Browse files
feat: add rtl support on iOS
1 parent 0db5cfe commit a39660f

3 files changed

Lines changed: 51 additions & 8 deletions

File tree

ios/EnrichedTextInputView.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#import "InputParser.h"
66
#import "BaseStyleProtocol.h"
77
#import "InputTextView.h"
8+
#import "RCTI18nUtil.h"
89

910
#ifndef EnrichedTextInputViewNativeComponent_h
1011
#define EnrichedTextInputViewNativeComponent_h

ios/EnrichedTextInputView.mm

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ - (void)updateProps:(Props::Shared const &)props oldProps:(Props::Shared const &
415415
defaultTypingAttributes[NSFontAttributeName] = [config primaryFont];
416416
defaultTypingAttributes[NSUnderlineColorAttributeName] = [config primaryColor];
417417
defaultTypingAttributes[NSStrikethroughColorAttributeName] = [config primaryColor];
418-
defaultTypingAttributes[NSParagraphStyleAttributeName] = [[NSParagraphStyle alloc] init];
418+
defaultTypingAttributes[NSParagraphStyleAttributeName] = [self prepareDefaultParagraphStyle];
419419
textView.typingAttributes = defaultTypingAttributes;
420420

421421
// update the placeholder as well
@@ -522,6 +522,22 @@ - (void)updateProps:(Props::Shared const &)props oldProps:(Props::Shared const &
522522
}
523523
}
524524

525+
- (NSMutableParagraphStyle * )prepareDefaultParagraphStyle
526+
{
527+
NSMutableParagraphStyle *pStyle = [[NSMutableParagraphStyle alloc] init];
528+
529+
BOOL isRTL = [[RCTI18nUtil sharedInstance] isRTL];
530+
531+
if (isRTL) {
532+
pStyle.baseWritingDirection = NSWritingDirectionRightToLeft;
533+
} else {
534+
pStyle.baseWritingDirection = NSWritingDirectionLeftToRight;
535+
}
536+
pStyle.baseWritingDirection = NSWritingDirectionRightToLeft;
537+
538+
return pStyle;
539+
}
540+
525541
- (void)setPlaceholderLabelShown:(BOOL)shown {
526542
if(shown) {
527543
[self refreshPlaceholderLabelStyles];

ios/utils/LayoutManagerExtension.mm

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,17 @@ - (void)my_drawBackgroundForGlyphRange:(NSRange)glyphRange atPoint:(CGPoint)orig
6565
NSRange paragraphGlyphRange = [self glyphRangeForCharacterRange:paragraphRange actualCharacterRange:nullptr];
6666
[self enumerateLineFragmentsForGlyphRange:paragraphGlyphRange
6767
usingBlock:^(CGRect rect, CGRect usedRect, NSTextContainer * _Nonnull textContainer, NSRange glyphRange, BOOL * _Nonnull stop) {
68-
CGFloat paddingLeft = origin.x;
69-
CGFloat paddingTop = origin.y;
70-
CGFloat x = paddingLeft;
71-
CGFloat y = paddingTop + rect.origin.y;
68+
BOOL isRTL = [[RCTI18nUtil sharedInstance] isRTL];
7269
CGFloat width = [typedInput->config blockquoteBorderWidth];
7370
CGFloat height = rect.size.height;
71+
72+
CGFloat x = 0;
73+
if (isRTL) {
74+
x = origin.x + rect.size.width - width;
75+
} else {
76+
x = origin.x;
77+
}
78+
CGFloat y = origin.y + rect.origin.y;
7479

7580
CGRect lineRect = CGRectMake(x, y, width, height);
7681
[[typedInput->config blockquoteBorderColor] setFill];
@@ -103,18 +108,34 @@ - (void)my_drawBackgroundForGlyphRange:(NSRange)glyphRange atPoint:(CGPoint)orig
103108
[self enumerateLineFragmentsForGlyphRange:paragraphGlyphRange
104109
usingBlock:^(CGRect rect, CGRect usedRect, NSTextContainer *container, NSRange lineGlyphRange, BOOL *stop) {
105110
NSString *marker = [self markerForList:pStyle.textLists.firstObject charIndex:[self characterIndexForGlyphAtIndex:lineGlyphRange.location] input:typedInput];
111+
BOOL isRTL = [[RCTI18nUtil sharedInstance] isRTL];
112+
113+
CGFloat textStartLeft = usedRect.origin.x;
114+
CGFloat textEndRight = usedRect.origin.x + usedRect.size.width;
106115

107116
if(pStyle.textLists.firstObject.markerFormat == NSTextListMarkerDecimal) {
108117
CGFloat gapWidth = [typedInput->config orderedListGapWidth];
109118
CGFloat markerWidth = [marker sizeWithAttributes:markerAttributes].width;
110-
CGFloat markerX = usedRect.origin.x - gapWidth - markerWidth/2;
119+
CGFloat markerX = 0;
120+
121+
if (isRTL) {
122+
markerX = textEndRight + gapWidth;
123+
} else {
124+
markerX = textStartLeft - gapWidth - markerWidth / 2;
125+
}
111126

112127
[marker drawAtPoint:CGPointMake(markerX, usedRect.origin.y + origin.y) withAttributes:markerAttributes];
113128
} else {
114129
CGFloat gapWidth = [typedInput->config unorderedListGapWidth];
115130
CGFloat bulletSize = [typedInput->config unorderedListBulletSize];
116-
CGFloat bulletX = usedRect.origin.x - gapWidth - bulletSize/2;
117131
CGFloat centerY = CGRectGetMidY(usedRect);
132+
CGFloat bulletX = 0;
133+
134+
if (isRTL) {
135+
bulletX = textEndRight + gapWidth + (bulletSize / 2.0);
136+
} else {
137+
bulletX = textStartLeft - gapWidth - (bulletSize / 2.0);
138+
}
118139

119140
CGContextRef context = UIGraphicsGetCurrentContext();
120141
CGContextSaveGState(context); {
@@ -161,8 +182,13 @@ - (NSString *)markerForList:(NSTextList *)list charIndex:(NSUInteger)index input
161182

162183
itemNumber = prevParagraphsCount + 1;
163184
}
185+
BOOL isRTL = [[RCTI18nUtil sharedInstance] isRTL];
164186

165-
return [NSString stringWithFormat:@"%ld.", (long)(itemNumber)];
187+
if (isRTL) {
188+
return [NSString stringWithFormat:@".%ld", (long)(itemNumber)];
189+
} else {
190+
return [NSString stringWithFormat:@"%ld.", (long)(itemNumber)];
191+
}
166192
} else {
167193
return @"";
168194
}

0 commit comments

Comments
 (0)