question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow for Text components to support a forced minimum number of lines (numberOfLines)

See original GitHub issue

For Discussion

As of 0.57.0, the Text component’s numberOfLines prop is used to define the maximum number of lines of a string that will be rendered before truncation occurs. This is a bit of a misnomer, as one may anticipate that this prop w

Should this prop be renamed to something such as maxNumberOfLines? Would the maintainers consider the possibility of adding an additional prop, such as minNumberOfLines, which would force the Text component to occupy the minimum space necessary to render the defined amount of lines?

Motivation

Both Android and iOS’s interface guidelines specify that when producing a tappable list of items (such as a list of messages) these items should be of even height.

In order to maintain accessible support for font scaling, a cell’s height should be inferred from the height of the content, however, it is currently impossible to achieve a consistent inferred height from Text’s available props.

Current Behavior

These cells do not have a consistent height simulator screen shot - iphone xs max - 2018-09-28 at 13 45 35

Anticipated Behavior

These cells have a consistent height simulator screen shot - iphone xs max - 2018-09-28 at 13 52 45

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:10
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
vovkasmcommented, Oct 3, 2018

I personally think that in the example, cell height should be fixed, and contained elements should layout according to available space…

2reactions
B3rrycommented, Oct 9, 2018

@vovkasm

cell height should be set on View

Are you saying a calculated height should be applied directly to the View to achieve this? Such as:

 <View style={{height: SomeCalculatedHeight}}>

Yes, this is an option, but at that point you’re still not allowing for standard Flex principles to create a consistently sized text component in a dynamic layout. Is there any reason why consistent, inferred heights should only be available through calculation?

See, you set minNumberOfLines, but text not fit in width… you cell will be higher than others…

Unsure what you mean.

how you set constraints that all cells has the same size and at the same time wrap their content?

This is already a factor of numberOfLines. It does exactly that: defines a set height, wraps the text. Additionally, it truncates text when it runs over. I’d imagine setting a unique offset (bump up the padding?) on each text container that does not meet the calculated size of a text container with n lines could work.

minimumTextContainerHeight = heightOfSingleLine * minimumNumberOfLines + totalVerticalPadding

verticalPaddingToMeetMinimumHeight = minimumTextContainerHeight - (heightOfSingleLine * numberOfRenderedLines)

There’s probably some considerations around how to take around how you get to some of these values, especially given font scaling and line-height inconsistencies between render in iOS and android. I’m unsure of the full scope of these.

Okey, you set minNumberOfLines and maxNumberOfLines (to same value, or cells again will not be same height)… how then you set size for you icons on left side? Etc… etc…

You could use the Flex, nativeEvent and Dimensions achieve this. That’s how it’s done now. Although normal behavior for iOS elements is to not scale icons and images when the text does (in most cases). If you really wanted to, you could use PixelRatio.getFontScale() to scale the Entire UI, or handle responsiveness issues.

This process seems rather complex to produce a consistently sized, responsive component. This is a very common UX pattern in iOS and Android, and I don’t think relying on each developer to implement their own solution is best, in this case.

Read more comments on GitHub >

github_iconTop Results From Across the Web

React native text component using number of lines
You can use numberOfLines as a props for <Text /> component. Its depend on the width of your component then calculate the length...
Read more >
Text - React Native
numberOfLines ​. Used to truncate the text with an ellipsis after computing the text layout, including line wrapping, such that the total number...
Read more >
-webkit-line-clamp - CSS: Cascading Style Sheets | MDN
The -webkit-line-clamp CSS property allows limiting of the contents of a block to the specified number of lines.
Read more >
TextInput · React Native
A foundational component for inputting text into the app via a keyboard. ... This property is supported only for single-line TextInput component.
Read more >
Support text scaling - Medium
If you need a line limit, you can set a different number of lines for the default and for the larger sizes for...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found