Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React native text word break strategy of strings with dash

Setup

react: 16.6.0-alpha.8af6728
react-native: 0.57.4

Problem

Word breaking in Text component does not handle strings with dashes the way app design wants it. I want to word wrap the whole word taking into account dashes. This whole string-with-dashes should be considered a word when word wrapping. But flexbox does not.

Code

<TouchableOpacity style={{ width: 250, backgroundColor: 'skyblue' }}>
    <Text style={styles.welcome}>This is a sample of text-with-dash incorrectly word breaking</Text>
</TouchableOpacity>

Restult looks like this:

enter image description here

But I want it to end up like this (text-with-dash on a seperate line):

enter image description here

The issue is I get the strings from an online CMS and want a flexbox styling solution to this problem. There might be situation where a string-with-dash could end up in a single line so in those instances I don't want a word wrap of cause.

like image 875
hdsenevi Avatar asked Nov 13 '18 11:11

hdsenevi


People also ask

How do you break a text line in react native?

You can use \n where you want to break the line.

How do you stop text from overflowing in react native?

The only way to achieve this in React Native is to set position: absolute on the Text element on a flex-row container - quite the struggle, and definitely not the default...

How do you wrap text in TextInput in react native?

To achieve the same effect, you can wrap your TextInput in a View : import React, { Component } from 'react'; import { AppRegistry, View, TextInput } from 'react-native'; class UselessTextInput extends Component { render() { return ( <TextInput {...

How do you use gap in react native?

Using flexbox's gap in React Native Below, let's set the container's gap property to 1rem to add spacing of 1rem between items horizontally and vertically: import React, { useState } from "react"; import { StyleSheet, View } from "react-native"; export default function App() { return ( <> <View style={styles.


2 Answers

Use unicode \u2011 of non breaking hyphen in string. So, for your example it would look like this:

<TouchableOpacity style={{ width: 250, backgroundColor: 'skyblue' }}>
    <Text style={styles.welcome}>{`This is a sample of text\u2011with\u2011dash incorrectly word breaking`}</Text>
</TouchableOpacity>
like image 80
Peretz30 Avatar answered Oct 11 '22 21:10

Peretz30


You can now use the textbreakstrategy as props of Text.

By default, the text break strategy is 'highQuality' which breaks the words and appends the '-' for those words.

Use 'simple' in the textbreakstrategy to avoid the '-' when breaking words.

For example:

<Text textBreakStrategy={'simple'}>
This is a sample of text-with-dash incorrectly word breaking
</Text>

Additional Reference: https://developer.android.com/reference/android/text/Layout.html#BREAK_STRATEGY_SIMPLE

like image 3
Chong Zheng Avatar answered Oct 11 '22 20:10

Chong Zheng