score:1
Using lineHeight
in your styling should do what you're looking for:
<Text>
<Text style={{color: '#fff', fontWeight: '600', fontSize: 26, backgroundColor: 'blue', lineHeight: 20 }}>
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
</Text>
score:1
I was able to achieve this effect using a solution similar to what Travis White suggests above. Image of effect in action
It is by no means a perfect solution, and I'm almost certain there are use cases that I am not handling for, but here it is:
class HighlightText extends Component {
state = { lines: [] };
renderLines() {
let textString = this.props.children;
// splitOn is integer value. Enter expected max char count per line as prop
const splitOn = this.props.splitOn;
// Adds space to end of string, preventing cutoff of last word
const singleSpace = ' ';
textString = textString.concat(singleSpace);
const numOfLines = Math.ceil(textString.length / splitOn);
let lineStart = 0;
let lineEnd = textString.slice(0, splitOn).lastIndexOf(' ');
let fakeLineEnd = lineStart + splitOn;
/* multiplying x2 to handle for awkward splits before very long words
that can push content beyond the above calculated numOfLines */
for (i = 0; i < numOfLines * 2; i++) {
let line = textString.slice(lineStart, lineEnd);
// Adds spaces to start and end of already populated lines for visual padding
if (line.length > 0) {
line = singleSpace + line + singleSpace;
this.state.lines.push(line);
}
lineStart = lineEnd + 1;
fakeLineEnd = lineStart + splitOn;
lineEnd = textString.slice(0, fakeLineEnd).lastIndexOf(' ');
}
return this.state.lines.map((line, i) =>
<View
key={i}
style={{
marginTop: this.props.marginTop,
}}
>
<Text>
<Text
style={{
fontSize: this.props.fontSize,
color: this.props.color,
backgroundColor: this.props.backgroundColor
}}
>
{line}
</Text>
</Text>
</View>
);
}
render() {
return (
<View>
{this.renderLines()}
</View>
);
}
}
Source: stackoverflow.com
Related Query
- React Native - text with background and a gap between multiple lines
- React Native Inline style for multiple Text in single Text With Touch effect
- How to render a div component multiple times with different background color and text in React?
- Calculate lines of text in javascript in React with Hooks and redux
- React - 2-way bind text input with format different between stored and displayed
- What is the difference between React Native and React?
- Multiple classNames with CSS Modules and React
- React native text component using number of lines
- Test setting text value with React and Enzyme
- React slow with multiple controlled text inputs
- Building hybrid React apps for iOS and Android with native performance
- Can we share code between react webapp and react native app and is react-native production ready
- React Native - What is the difference between StyleSheet.absoluteFill() and StyleSheet.absoluteFillObject()?
- Difference between with and without useEffect in react functional component
- Center a text in the screen with react native
- react native post form data with object and file in it using axios
- How to use vw and vh css with React Native
- React Create a Horizontal Divider with Text In between
- React Native - Difference between onChangeText and onSubmitEditing?
- How to limit the text filed length with input type number in React JS and prevent entry of E,e, -, + etc
- Multiple Registration Form with redux and react
- Test text input with react and jest
- How to nest Scene and navigate with direction='vertical' in React Native Router Flux?
- Change the background color of a screen with react native
- What is the difference between accessible, accessibilityLabel and accessibilityHint properties of Text component in react native?
- React Native Web with Next JS and React Navigation
- In Visual Studio 2012, I get warnings and squiggly lines when trying to write JSX with React JS
- How to create multiple react native apps with same functionality
- Save and retrieve states with redux-persist - React Native
- How to apply react-native-linear-gradient for the entire app background with React Native
More Query from same tag
- How can I use history.push('path') in react router 5.1.2 in stateful component?
- saving userType in ReactJs thorughout the session
- ReactJs lazy when trying to use lazy loading, getting 'window; is not defined error
- × TypeError: Cannot read property 'dishes' of undefined
- React w/Typescript: useReducer, Action Interface w/ Union Type
- Errors show correctly on Graphiql but not returned to Apollo Client in react front end
- Unexpected token (8:8) while parsing file
- how to call `localStorage.getItem ` immediately after render
- React Testing Library - Screen debug gives output different from browser
- How to avoid repeating JSX / HTML code in react?
- Trying to mapping two different arrays coming from an API to an Object
- How to use selectById selector generated from createEntityAdapter
- What is the best approach to embed a hosted React application in to another non React site?
- Check if value is negative and apply CSS in React JS
- Default Props in stateful (using hooks) functional components
- Why use Next.js API route with an external API?
- React binding method in constructor - how to pass in Argument
- KendoReact Grid Checkbox Filter
- How to set state using Hooks?
- How to close an IonPopover in Ionic/React?
- React hooks: maximum depth exceeded
- Information remaining across profiles when signing in on the same device reactJS
- Triggering react tests inside docker containers
- Error using react-toastify, toast do not view
- React Apollo: How to access element Trix-Editor within a Mutation component?
- setFieldValue, Formik and InvalidStateError: Failed to set the 'value' property on 'HTMLInputElement'
- How do I set a delay on a file upload in React?
- can't access to updated state with appanded element
- useEffect only if state changed
- How to make Node and React app to share classes