I'm having a long text in my app and I need to truncate it and add three dots to the end.
How can I do that in React Native Text element?
Thanks
I'm having a long text in my app and I need to truncate it and add three dots to the end.
How can I do that in React Native Text element?
Thanks
use numberOfLines
<Text numberOfLines={1}>long long long long text<Text>
or if you know/or can compute the max character count per row, JS substring may be used.
<Text>{ ((mytextvar).length > maxlimit) ?
(((mytextvar).substring(0,maxlimit-3)) + '...') :
mytextvar }
</Text>
<Text>
for handling the long text? As i am facing that issue for <ListItemText>
in material ui
and i am not able to find any answer for that –
Outwork numberOfLines
parameter on a Text
component:<Text numberOfLines={1}>long long long long text<Text>
Will produce:
long long long…
(Assuming you have short width container.)
ellipsizeMode
parameter to move the ellipsis to the head
or middle
. tail
is the default value.<Text numberOfLines={1} ellipsizeMode='head'>long long long long text<Text>
Will produce:
…long long text
NOTE: The Text
component should also include style={{ flex: 1 }}
when the ellipsis needs to be applied relative to the size of its container. Useful for row layouts, etc.
style={{ flexShrink: 1 }}
can be used instead of flex: 1
if you don't want the element to grow in a row layout –
Cann use numberOfLines
<Text numberOfLines={1}>long long long long text<Text>
or if you know/or can compute the max character count per row, JS substring may be used.
<Text>{ ((mytextvar).length > maxlimit) ?
(((mytextvar).substring(0,maxlimit-3)) + '...') :
mytextvar }
</Text>
<Text>
for handling the long text? As i am facing that issue for <ListItemText>
in material ui
and i am not able to find any answer for that –
Outwork You can use ellipsizeMode and numberOfLines. e.g
<Text ellipsizeMode='tail' numberOfLines={2}>
This very long text should be truncated with dots in the beginning.
</Text>
<Text ellipsizeMode='tail' numberOfLines={2} style={{width:100}}>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam at cursus
</Text>
Result inside box:
<-- width = 100-->
-----------------
| Lorem ipsum |
| dolar sit a... |
-----------------
<View
style={{
flexDirection: 'row',
padding: 10,
}}
>
<Text numberOfLines={5} style={{flex:1}}>
This is a very long text that will overflow on a small device This is a very
long text that will overflow on a small deviceThis is a very long text that
will overflow on a small deviceThis is a very long text that will overflow
on a small device
</Text>
</View>
To Achieve ellipses for the text use the Text property numberofLines={1} which will automatically truncate the text with an ellipsis you can specify the ellipsizeMode as "head", "middle", "tail" or "clip" By default it is tail
If you want read more behavior, then you can use the react-native-read-more-text
library:
npm i react-native-read-more-text --save
<ReadMore
numberOfLines={1}
renderTruncatedFooter={(handlePress) => { return <Text onPress={handlePress} style={{ color: 'grey' }}>show more</Text> }}
renderRevealedFooter={(handlePress) => { return <Text onPress={handlePress} style={{ color: 'grey' }}>show less</Text> }}
>
<Text>yourText</Text>
</ReadMore>
Docs: https://github.com/expo/react-native-read-more-text
To hide "read more" when the content is less than numberOfLines, you can use ternary operator:
{
'yourText'.length > 50
?
<ReadMore
numberOfLines={1}
renderTruncatedFooter={(handlePress) => { return <Text onPress={handlePress} style={{ color: 'grey' }}>show more</Text> }}
renderRevealedFooter={(handlePress) => { return <Text onPress={handlePress} style={{ color: 'grey' }}>show less</Text> }}
>
<Text>yourText</Text>
</ReadMore>
:
<Text>yourText</Text>
}
<Text numberOfLines={1}>long long long long text<Text>
exchangeTypeText: { width: "85%", overflow: "hidden",
},
Here is JSX vertion if some one using simple react ,, not knowing react native though
import { useState } from "react";
function ElipseText({ text, size = 500 }) {
const [showMore, setShowMore] = useState(true)
const renderText = (text) => {
let textJSX = text;
if (showMore) {
textJSX = text.substring(0, size);
}
return (<span className="elipse-text">
<p className="text01" dangerouslySetInnerHTML={{ __html: textJSX }} />
<a className="btn01" onClick={() => setShowMore(!showMore)}>
{!showMore && <svg width="1em" height="1em" viewBox="0 0 512 512"><path fill="currentColor" d="M497.333 239.999H80.092l95.995-95.995l-22.627-22.627L18.837 256L153.46 390.623l22.627-22.627l-95.997-95.997h417.243v-32z"></path></svg>}
{showMore ? "Show More" : "Show Less"}
{showMore && <svg width="1em" height="1em" viewBox="0 0 15 15"><path fill="currentColor" fillRule="evenodd" d="M9.854 3.146L14.207 7.5l-4.353 4.354l-.708-.708L12.293 8H1V7h11.293L9.146 3.854l.708-.708Z" clipRule="evenodd"></path></svg>}
</a>
</span>)
}
return (
<>
{renderText(text)}
</>
)
}
export default ElipseText
SCSS File
.elipse-text {
.btn01 {
display: inline-flex;
color: var(--color-dark);
align-items: center;
gap: 0.5rem;
border-bottom: 1px solid var(--color-dark);
}
.text01 {
display: contents;
}
}
const styles = theme => ({
contentClass:{
overflow: 'hidden',
textOverflow: 'ellipsis',
display: '-webkit-box',
WebkitLineClamp:1,
WebkitBoxOrient:'vertical'
}
})
render () {
return(
<div className={classes.contentClass}>
{'content'}
</div>
)
}
© 2022 - 2024 — McMap. All rights reserved.
<Text>
andnumberOfLines
– Waggoner