Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[iOS] Keep placeholder paragraph style same with text input #23763

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions Libraries/Text/RCTTextAttributes.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ extern NSString *const RCTTextAttributesTagAttributeName;
*/
- (NSDictionary<NSAttributedStringKey, id> *)effectiveTextAttributes;

/**
* Constructed paragraph style.
*/
- (NSParagraphStyle *_Nullable)effectiveParagraphStyle;

/**
* Constructed font.
*/
Expand Down
69 changes: 40 additions & 29 deletions Libraries/Text/RCTTextAttributes.m
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,44 @@ - (void)applyTextAttributes:(RCTTextAttributes *)textAttributes
_textTransform = textAttributes->_textTransform != RCTTextTransformUndefined ? textAttributes->_textTransform : _textTransform;
}

- (NSParagraphStyle *)effectiveParagraphStyle
{
// Paragraph Style
NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
BOOL isParagraphStyleUsed = NO;
if (_alignment != NSTextAlignmentNatural) {
NSTextAlignment alignment = _alignment;
if (_layoutDirection == UIUserInterfaceLayoutDirectionRightToLeft) {
if (alignment == NSTextAlignmentRight) {
alignment = NSTextAlignmentLeft;
} else if (alignment == NSTextAlignmentLeft) {
alignment = NSTextAlignmentRight;
}
}

paragraphStyle.alignment = alignment;
isParagraphStyleUsed = YES;
}

if (_baseWritingDirection != NSWritingDirectionNatural) {
paragraphStyle.baseWritingDirection = _baseWritingDirection;
isParagraphStyleUsed = YES;
}

if (!isnan(_lineHeight)) {
CGFloat lineHeight = _lineHeight * self.effectiveFontSizeMultiplier;
paragraphStyle.minimumLineHeight = lineHeight;
paragraphStyle.maximumLineHeight = lineHeight;
isParagraphStyleUsed = YES;
}

if (isParagraphStyleUsed) {
return [paragraphStyle copy];
}

return nil;
}

- (NSDictionary<NSAttributedStringKey, id> *)effectiveTextAttributes
{
NSMutableDictionary<NSAttributedStringKey, id> *attributes =
Expand Down Expand Up @@ -107,35 +145,8 @@ - (void)applyTextAttributes:(RCTTextAttributes *)textAttributes
}

// Paragraph Style
NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
BOOL isParagraphStyleUsed = NO;
if (_alignment != NSTextAlignmentNatural) {
NSTextAlignment alignment = _alignment;
if (_layoutDirection == UIUserInterfaceLayoutDirectionRightToLeft) {
if (alignment == NSTextAlignmentRight) {
alignment = NSTextAlignmentLeft;
} else if (alignment == NSTextAlignmentLeft) {
alignment = NSTextAlignmentRight;
}
}

paragraphStyle.alignment = alignment;
isParagraphStyleUsed = YES;
}

if (_baseWritingDirection != NSWritingDirectionNatural) {
paragraphStyle.baseWritingDirection = _baseWritingDirection;
isParagraphStyleUsed = YES;
}

if (!isnan(_lineHeight)) {
CGFloat lineHeight = _lineHeight * self.effectiveFontSizeMultiplier;
paragraphStyle.minimumLineHeight = lineHeight;
paragraphStyle.maximumLineHeight = lineHeight;
isParagraphStyleUsed = YES;
}

if (isParagraphStyleUsed) {
NSParagraphStyle *paragraphStyle = [self effectiveParagraphStyle];
if (paragraphStyle) {
attributes[NSParagraphStyleAttributeName] = paragraphStyle;
}

Expand Down
7 changes: 2 additions & 5 deletions Libraries/Text/TextInput/Multiline/RCTUITextView.m
Original file line number Diff line number Diff line change
Expand Up @@ -257,11 +257,8 @@ - (void)invalidatePlaceholderVisibility
NSForegroundColorAttributeName: self.placeholderColor ?: defaultPlaceholderColor(),
NSKernAttributeName:isnan(_reactTextAttributes.letterSpacing) ? @0 : @(_reactTextAttributes.letterSpacing)
}];
if (!isnan(_reactTextAttributes.lineHeight)) {
NSMutableParagraphStyle *paragraphStyle = [NSMutableParagraphStyle new];
CGFloat lineHeight = _reactTextAttributes.lineHeight * _reactTextAttributes.effectiveFontSizeMultiplier;
paragraphStyle.minimumLineHeight = lineHeight;
paragraphStyle.maximumLineHeight = lineHeight;
NSParagraphStyle *paragraphStyle = [_reactTextAttributes effectiveParagraphStyle];
if (paragraphStyle) {
effectiveTextAttributes[NSParagraphStyleAttributeName] = paragraphStyle;
}

Expand Down