[英]React Native limit characters
I am working with the API.我正在使用 API。
renderGridItem = (itemData) => {
return(
<Text>
{itemData.item.description}
</Text>
);
};
I want to show only the first 50 characters of the description.我只想显示描述的前 50 个字符。 How can I do that?
我怎样才能做到这一点?
Welcome @Nai to StackOverflow,欢迎@Nai 来到 StackOverflow,
You may use the pure JavaScript functions.您可以使用纯 JavaScript 函数。
slice
or maybe substring
. slice
或者可能substring
。
Use of Slice:-切片的使用:-
renderGridItem = (itemData) => {
return(
<Text>
{itemData.item.description.slice(0, 50)}
</Text>
);
};
Not exactly 50 characters, but you could achieve this with a combination of:不完全是 50 个字符,但您可以通过以下组合来实现:
numberOfLines
prop for text and set it to 1 / 2 (depending on your needs).numberOfLines
属性并将其设置为 1 / 2(取决于您的需要)。ellipsizeMode
with whatever you want it to be.ellipsizeMode
。 Link to docs on Text in React Native.链接到有关 React Native 中文本的文档。
This way you're making sure that your text is going to show correctly and no word is going to be cut in an ugly way.这样,您可以确保您的文本将正确显示,并且不会以丑陋的方式剪切任何单词。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.