{"id":9373,"date":"2024-01-23T11:44:56","date_gmt":"2024-01-23T11:44:56","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/qanda\/?p=9373"},"modified":"2024-01-29T09:10:37","modified_gmt":"2024-01-29T09:10:37","slug":"react-native-text-going-off-screen","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/qanda\/react-native\/react-native-text-going-off-screen","title":{"rendered":"React Native Text Going Off Screen, Refusing to Wrap. What to Do?"},"content":{"rendered":"<p>When dealing with long text in React Native that is going off the screen and refusing to wrap, there are a few approaches you can take to address the issue.<\/p>\n<p>Let&#8217;s create a basic screen to showcase a few solutions for managing long text that doesn&#8217;t wrap as expected.<\/p>\n<h3>Solution 1: Width Limitation<\/h3>\n<p>You can set a maxWidth on the Text component to limit its width.<br \/>\nExample :<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"r\">\r\n&lt;Text style={{ maxWidth: '80%' }}&gt;Your long text here&lt;\/Text&gt;<\/pre>\n<p>This allows the text to take up a maximum of 80% of its parent&#8217;s width.<\/p>\n<p>The below code shows how we can achieve this by giving maxWidth:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"r\">\r\nimport React from 'react';\r\nimport {View, Text, StyleSheet} from 'react-native';\r\nconst App = () => {\r\n return (\r\n   <View style={styles.container}>\r\n     <View style={[styles.textContainer, {backgroundColor: 'blue'}]}>\r\n       <View style={styles.descriptionContainer}>\r\n         <Text style={styles.descriptionText} numberOfLines={5}>\r\n           Lorem Ipsum is simply dummy text of the printing and typesetting\r\n           industry. Lorem Ipsum has been the industry's standard dummy text\r\n           ever since the 1500s, when an unknown printer took a galley of type\r\n           and scrambled it to make a type specimen book.\r\n         <\/Text>\r\n       <\/View>\r\n     <\/View>\r\n     <View style={[styles.textContainer, {backgroundColor: 'orange'}]}>\r\n       <View style={styles.descriptionContainer}>\r\n         <Text style={styles.descriptionText} numberOfLines={5}>\r\n           Lorem Ipsum is simply dummy text of the printing and typesetting\r\n           industry.\r\n         <\/Text>\r\n       <\/View>\r\n     <\/View>\r\n   <\/View>\r\n );\r\n};\r\n\r\nconst styles = StyleSheet.create({\r\n container: {\r\n   flex: 1,\r\n },\r\n textContainer: {\r\n   flex: 0.5,\r\n   justifyContent: 'center', \/\/ Center children vertically\r\n   alignItems: 'center', \/\/ Center children horizontally\r\n   paddingHorizontal: '15%',\r\n },\r\n descriptionContainer: {\r\n   flexDirection: 'row', \/\/ Stack children in a row\r\n },\r\n descriptionText: {\r\n   fontSize: 16,\r\n   color: 'white',\r\n   textAlign: 'center',\r\n   maxWidth: '80%', \/\/ 80% of the parent's width\r\n   backgroundColor: 'green',\r\n },\r\n});\r\nexport default App;\r\n<\/pre>\n<h3>Solution 2: Flexbox<\/h3>\n<p>To address the issue of text going off the screen and refusing to wrap using Flexbox, you can structure your components appropriately. In this example, the flex: 0.8 property for the longText style ensures that the text occupies 80% of its parent&#8217;s width. Adjust this value based on your specific layout requirements. <\/p>\n<p>Here&#8217;s a code:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"r\">\r\nimport React from 'react';\r\nimport {StyleSheet, Text, View} from 'react-native';\r\n\r\n\r\nfunction App(): React.JSX.Element {\r\n return (\r\n   <View style={styles.container}>\r\n     <View style={[styles.textWrap, {backgroundColor: 'blue'}]}>\r\n       <Text style={styles.descriptionText} numberOfLines={5}>\r\n         Lorem Ipsum is simply dummy text of the printing and typesetting\r\n         industry. Lorem Ipsum has been the industry's standard dummy text ever\r\n         since the 1500s, when an unknown printer took a galley of type and\r\n         scrambled it to make a type specimen book.\r\n       <\/Text>\r\n     <\/View>\r\n     <View style={[styles.textWrap, {backgroundColor: 'orange'}]}>\r\n       <Text style={styles.descriptionText} numberOfLines={5}>\r\n         Lorem Ipsum is simply dummy text of the printing and typesetting\r\n         industry.\r\n       <\/Text>\r\n     <\/View>\r\n   <\/View>\r\n );\r\n}\r\n\r\nconst styles = StyleSheet.create({\r\n container: {\r\n   flex: 1,\r\n },\r\n descriptionText: {\r\n   backgroundColor: 'green',\r\n   fontSize: 16,\r\n   color: 'white',\r\n   textAlign: 'center',\r\n   flex: 0.8,\r\n },\r\n textWrap: {\r\n   flex: 0.5,\r\n   alignItems: 'center',\r\n   justifyContent: 'center',\r\n   flexDirection: 'row',\r\n   paddingHorizontal: '15%',\r\n },\r\n});\r\n\r\nexport default App;\r\n\r\n<\/pre>\n<p>Both solutions are suitable for Android and iOS, choose the one that aligns with your design and functionality preferences.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When dealing with long text in React Native that is going off the screen and refusing to wrap, there are a few approaches you can take to address the issue. Let&#8217;s create a basic screen to showcase a few solutions for managing long text that doesn&#8217;t wrap as expected. Solution 1: Width Limitation You can [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":9374,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[14],"tags":[],"class_list":["post-9373","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-react-native"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/9373"}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/comments?post=9373"}],"version-history":[{"count":3,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/9373\/revisions"}],"predecessor-version":[{"id":9377,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/9373\/revisions\/9377"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media\/9374"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media?parent=9373"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/categories?post=9373"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/tags?post=9373"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}