react-native: [Text] Text doesn't wrap
http://puu.sh/i407j/805ec722a0.png Text doesn’t wrap when it’s wrapped inside :
msgReceivedAlignContainer: {
flex: 1,
flexDirection: 'column',
alignItems: 'flex-w',
},
About this issue
- Original URL
- State: closed
- Created 9 years ago
- Comments: 29 (1 by maintainers)
Links to this issue
Commits related to this issue
- Fixed bug in iOs Solution found in this link: https://github.com/facebook/react-native/issues/1438 — committed to hulkdx/react-native-crod by hulkdx 7 years ago
- Fix incorrect wrapping in TermsAndConditionsScreen This commit fixes a visual bug that caused text not to wrap properly for some paragraphs. I was able to solve this thanks to this thread: <https://... — committed to blockfirm/pine-app by timothyej 6 years ago
Hey there @mvnnn, I was able to get this to work by adding a ‘flex: 1’ property to the text element. Hopefully it works in your situation, as well.
<View style={{flexDirection:'row'}}> <Text style={{flex: 1, flexWrap: 'wrap'}}> You miss fdddddd dddddddd You miss fdddddd dddddddd</Text> </View>
I tried @a11y1337 's method and it didn’t work for me. I have a situation where I have text sitting in a
View
along side of anotherView
and the text is running through the parent’spadding
and out of the parent’s container:Markup:
Styles:
As I said, I tried all of the the suggestions here and nothing seemed to work. WHAT DID WORK was adding a
width: 0
to theinfo
style:and voilà:
Try add
flexWrap: "wrap"
to the style of the Text componentAfter a bit of hard time I’ve found out that adding
flexShrink: 1
to text container helped.@mvnnn You have to add it to your <Text> component, like:
@bogdantmm92 - the problem is that it won’t wrap because the
flexDirection
is row - it just continues flowing in that direction rather than downwards (as it would with column). I fixed this for you in this example on rnplay - thanks for providing an excellent sample! Makes the issue much easier to address@dabit1 thanks!
Example that works:
Code
Styles
I think this also work
<View style={{flexDirection:'row', flex: 1, flexWrap: 'wrap'}}> <Text> You miss fdddddd dddddddd You miss fdddddd dddddddd</Text> </View>
Hi all,
Text supports
NumberOfLines
property, not sure about the case described above, but maybe it can be an option.All of the above solutions didn’t work for me, I had a fairly complicated view hierarchy, simpler versions always worked as expected.
Also, I was using
react-native-web
, so coding toreact-native
standards, usingView
andText
components as the basis for all rendering. The above did allow me to resolve the issue on Native, but it didn’t resolve it on Web.The correct solution (for me) was to ensure that I had
flex: 1
on ALL ancestors of theText
node that was not wrapping as expected.So - check your view hierarchy, you can do this in the browser using the Element Inspector (Computed Styles), just walk up from the non-wrapping node and check for
display: flex
- anything missingflex
is missing the correspondingflex: 1
.NB:
Text
nodes inreact-native-web
do not pass theflex
property to the underlyingdiv
/span
.So far @gusgard answer always worked for me but I just faced this issue and despite all of the documented solution no one actually worked except for that one :
I found this gem https://stackoverflow.com/questions/38233789/react-native-view-auto-width-by-text-inside. the accepted answer tells us that 👍
I don’t know you but I really didn’t know. Just tested it and it worked like a charm.
this worked for me:
@pie6k You saved my day. Many thanks for your sharing!
@IrvanWijaya read this guide which is probably implemented the same way on React Native.
@a11y1337 … that saved me ton of headache! thank you.
@dwilt That helped!