diff --git a/Libraries/Text/RCTTextAttributes.h b/Libraries/Text/RCTTextAttributes.h index 01070aa4e21..d48b10b131b 100644 --- a/Libraries/Text/RCTTextAttributes.h +++ b/Libraries/Text/RCTTextAttributes.h @@ -66,6 +66,11 @@ extern NSString *const RCTTextAttributesTagAttributeName; */ - (NSDictionary *)effectiveTextAttributes; +/** + * Constructed paragraph style. + */ +- (NSParagraphStyle *_Nullable)effectiveParagraphStyle; + /** * Constructed font. */ diff --git a/Libraries/Text/RCTTextAttributes.m b/Libraries/Text/RCTTextAttributes.m index 7da187fc341..15b6851ed96 100644 --- a/Libraries/Text/RCTTextAttributes.m +++ b/Libraries/Text/RCTTextAttributes.m @@ -79,6 +79,44 @@ NSString *const RCTTextAttributesTagAttributeName = @"RCTTextAttributesTagAttrib _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 *)effectiveTextAttributes { NSMutableDictionary *attributes = @@ -107,35 +145,8 @@ NSString *const RCTTextAttributesTagAttributeName = @"RCTTextAttributesTagAttrib } // 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; } diff --git a/Libraries/Text/TextInput/Multiline/RCTUITextView.m b/Libraries/Text/TextInput/Multiline/RCTUITextView.m index 871bfe0d467..4ff8cc90ef4 100644 --- a/Libraries/Text/TextInput/Multiline/RCTUITextView.m +++ b/Libraries/Text/TextInput/Multiline/RCTUITextView.m @@ -257,11 +257,8 @@ static UIColor *defaultPlaceholderColor() 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; }