score:169
you can get this effect by wrapping text elements in other text elements the way you would wrap a span in a div or another element:
<view>
<text><text>this writing should fill most of the container </text><text>this writing should fill most of the container</text></text>
</view>
you can also get this effect by declaring a flexdirection:'row' property on the parent along with a flexwrap: 'wrap'. the children will then display inline:
<view style={{flexdirection:'row', flexwrap:'wrap'}}>
<text>one</text><text>two</text><text>three</text><text>four</text><text>five</text>
</view>
check out this example.
score:2
try this, simple and clean.
<text style={{ fontfamily: 'custom_font', ... }}>
<text>lorem ipsum</text>
<text style={{ color: "red" }}> dolor sit amet.</text>
</text>
result:
lorem ipsum dolor sit amet.
score:5
i had the following use case:
i needed a text that can wrap with different sizes, and throughout that text, i wanted to underscore some of the words (to indicate that they are clickable).
it's quite simple expect for the case that you can't control the underline in any way (how close is it, what color is it, so on) - this led me through the rabbit hole, and eventually coming up with the solution of splitting every word, and wrapping it in separate text component, wrapped with view.
i'll paste the code here:
import react from 'react';
import { stylesheet, view, touchableopacity, text } from 'react-native';
import colors from '../../styles/colors';
import fonts from '../../styles/fonts';
const styles = stylesheet.create({
container: {
flex: 1,
},
});
export default class salttext extends react.component {
gettheme (type) {
if (type === 'robomonoregular10gray') {
return {
fontsize: fonts.sizes.ten,
fontfamily: fonts.robotomono_regular,
color: colors.getcoloropacity(colors.gray, 70),
lineheight: fonts.sizes.ten + 10
};
}
throw new error('not supported');
}
splittext (text) {
const parts = [];
const maps = [];
let currentpart = '';
let matchindex = 0;
for (const letter of text) {
const isopening = letter === '[';
const isclosing = letter === ']';
if (!isopening && !isclosing) {
currentpart += letter;
continue;
}
if (isopening) {
parts.push(currentpart);
currentpart = '';
}
if (isclosing) {
parts.push(`[${matchindex}]`);
maps.push(currentpart);
currentpart = '';
matchindex++;
}
}
const partsmodified = [];
for (const part of parts) {
const splitted = part
.split(' ')
.filter(f => f.length);
partsmodified.push(...splitted);
}
return { parts: partsmodified, maps };
}
render () {
const textprops = this.gettheme(this.props.type);
const children = this.props.children;
const gettextstyle = () => {
return {
...textprops,
};
};
const gettextunderlinestyle = () => {
return {
...textprops,
borderbottomwidth: 1,
bordercolor: textprops.color
};
};
const getviewstyle = () => {
return {
flexdirection: 'row',
flexwrap: 'wrap',
};
};
const { parts, maps } = this.splittext(children);
return (
<view style={getviewstyle()}>
{parts.map((part, index) => {
const key = `${part}_${index}`;
const islast = parts.length === index + 1;
if (part[0] === '[') {
const mapindex = part.substring(1, part.length - 1);
const val = maps[mapindex];
const onpresshandler = () => {
this.props.onpress(parseint(mapindex, 10));
};
return (
<view key={key} style={gettextunderlinestyle()}>
<text style={gettextstyle()} onpress={() => onpresshandler()}>
{val}{islast ? '' : ' '}
</text>
</view>
);
}
return (
<text key={key} style={gettextstyle()}>
{part}{islast ? '' : ' '}
</text>
);
})}
</view>
);
}
}
and usage:
renderprivacy () {
const opentermsofservice = () => {
linking.openurl('https://reactnativecode.com');
};
const openprivacypolicy = () => {
linking.openurl('https://reactnativecode.com');
};
const onurlclick = (index) => {
if (index === 0) {
opentermsofservice();
}
if (index === 1) {
openprivacypolicy();
}
};
return (
<salttext type="robomonoregular10gray" onpress={(index) => onurlclick(index)}>
by tapping create an account or continue, i agree to salt\'s [terms of service] and [privacy policy]
</salttext>
);
}
this is the end result:
score:10
you can only nest text nodes without using flex to get the desired effect. like this: https://facebook.github.io/react-native/docs/text
<text style={{fontweight: 'bold'}}>
i am bold
<text style={{color: 'red'}}>
and red
</text>
</text>
score:11
i haven't found a proper way to inline text blocks with other content. our current "hackish" workaround is to split every single word in a text string into its own block so flexwrap wraps properly for each word.
Source: stackoverflow.com
Related Query
- Simulate display: inline in React Native
- React Native inline styles and performance
- How can I display dotted line in react native
- Access array of objects to display in React Native Text component
- Display Animated Value in React Native Render Text Component
- Looping Json & Display in React Native
- React Native Inline style for multiple Text in single Text With Touch effect
- How to display blob image in react native
- Display a <li> in reverse order in React project using inline CSS
- How in react to display svg icon from file as inline without making http request?
- How do you do an inline styling in React to display an unordered list as a result of using the map() method?
- Image display in react native android app
- How to display base64 images from array in React Native
- How to handle display none and block for a View component using react native
- Display native Map data in React
- How to save JS objects in array using react native AsyncStorage and display in a Flatlist
- Is there a way to display 3d models in a react native project without expo?
- How to calculate the distance between differents lat long and display on map dynamically react native
- how to throw id parameter and display detailed data based on id at listview in react native
- How to display date in firebase with react native
- Display value of returned var into render react native
- What is the difference between React Native and React?
- Error Running React Native App From Terminal (iOS)
- Setting a backgroundImage With React Inline Styles
- React native text going off my screen, refusing to wrap. What to do?
- React - Display loading screen while DOM is rendering?
- Get current scroll position of ScrollView in React Native
- How to display svg icons(.svg files) in UI using React Component?
- React Native Responsive Font Size
- Make view 80% width of parent in React Native
More Query from same tag
- How do I create a new JSON object inside a react hook?
- Edit object data from input value with hooks in React.js
- React Router -- history push state not refreshing with new state object
- Add new key value to Redux Toolkit state
- React map over collection from firebase
- Use state to replace complete object
- Malformed json in request body
- Add custom parameter to custom component "components" attribute
- this.setState() not updating state with passed array
- Webpack 5 Dev Server proxy seems to be ignored
- The Component view does not get updated but the redux store is getting updated
- React class is not defined (text/babel)
- How to fix "TS2349: Cannot invoke an expression whose type lacks a call signature"
- Replace node-sass by a platform agnostic SASS compiler in a React project
- Narrow union types for custom react hook
- TypeScript Parameter Defintion Overrides
- React-Router will not render components
- Map through array of objects
- Add to Home screen (A2HS) feature detection (no user agent sniffing)
- Why, after I removed the extra code, the list of names is not displayed?
- Pass prop from intermediate component into props.children
- Problem when my React App is put on the web
- Input search field created by React works incorrectly
- how to set Loading while api call in reactjs?
- Why does useState react hook cause Too many re-renders. React limits the number of renders to prevent an infinite loop?
- React component owning objects
- DRY react input handling causes cursor to jump
- How to prevent ArrowDown when reached bottom in react-select?
- ReactJS State and Local Variable
- E2E Testing Material-UI Select with Puppeteer