@@ -244,25 +244,48 @@ - (void)getRectWithAttributedString:(AttributedString)attributedString
244244
245245#pragma mark - Private
246246
247- - (NSTextStorage *)_textStorageForNSAttributesString : (NSAttributedString *)attributedString
247+ - (NSTextStorage *)_textStorageForNSAttributesString : (NSAttributedString *)inputAttributedString
248248 paragraphAttributes : (ParagraphAttributes)paragraphAttributes
249249 size : (CGSize)size
250250{
251- NSTextContainer *textContainer = [[NSTextContainer alloc ] initWithSize: size];
251+ NSMutableAttributedString *attributedString = [ inputAttributedString mutableCopy ];
252+
253+ /*
254+ * The block below is responsible for setting the exact height of the view in lines
255+ * Unfortunatelly, iOS doesn't export any easy way to do it. So we set maximumNumberOfLines
256+ * prop and then add random lines at the front. However, they are only used for layout
257+ * so they are not visible on the screen. This method is used for drawing only for Paragraph component
258+ * but we set exact height in lines only on TextInput that doesn't use it.
259+ */
260+ if (paragraphAttributes.numberOfLines ) {
261+ paragraphAttributes.maximumNumberOfLines = paragraphAttributes.numberOfLines ;
262+ NSMutableString *newLines = [NSMutableString stringWithCapacity: paragraphAttributes.numberOfLines];
263+ for (NSUInteger i = 0UL ; i < paragraphAttributes.numberOfLines ; ++i) {
264+ // K is added on purpose. New line seems to be not enough for NTtextContainer
265+ [newLines appendString: @" K\n " ];
266+ }
267+ NSDictionary <NSAttributedStringKey , id > * attributesOfFirstCharacter = [inputAttributedString attributesAtIndex: 0 effectiveRange: NULL ];
252268
253- textContainer. lineFragmentPadding = 0.0 ; // Note, the default value is 5.
254- textContainer. lineBreakMode = paragraphAttributes. maximumNumberOfLines > 0
255- ? RCTNSLineBreakModeFromEllipsizeMode (paragraphAttributes. ellipsizeMode )
256- : NSLineBreakByClipping;
257- textContainer. maximumNumberOfLines = paragraphAttributes. maximumNumberOfLines ;
269+
270+ [attributedString insertAttributedString: [[ NSAttributedString alloc ] initWithString: newLines attributes: attributesOfFirstCharacter] atIndex: 0 ];
271+ }
272+
273+ NSTextContainer * textContainer = [ NSTextContainer new ] ;
258274
259275 NSLayoutManager *layoutManager = [NSLayoutManager new ];
260276 layoutManager.usesFontLeading = NO ;
261277 [layoutManager addTextContainer: textContainer];
278+ NSTextStorage *textStorage = [NSTextStorage new ];
279+ [textStorage addLayoutManager: layoutManager];
262280
263- NSTextStorage *textStorage = [[NSTextStorage alloc ] initWithAttributedString: attributedString];
281+ textContainer.lineFragmentPadding = 0.0 ; // Note, the default value is 5.
282+ textContainer.lineBreakMode = paragraphAttributes.maximumNumberOfLines > 0
283+ ? RCTNSLineBreakModeFromEllipsizeMode (paragraphAttributes.ellipsizeMode )
284+ : NSLineBreakByClipping;
285+ textContainer.size = size;
286+ textContainer.maximumNumberOfLines = paragraphAttributes.maximumNumberOfLines ;
264287
265- [textStorage addLayoutManager: layoutManager ];
288+ [textStorage replaceCharactersInRange: ( NSRange ){ 0 , textStorage. length } withAttributedString: attributedString ];
266289
267290 if (paragraphAttributes.adjustsFontSizeToFit ) {
268291 CGFloat minimumFontSize = !isnan (paragraphAttributes.minimumFontSize ) ? paragraphAttributes.minimumFontSize : 4.0 ;
0 commit comments