Ani*_*ive 2 javascript text react-native react-native-flatlist
我正在开发本机应用程序。在那,我们展示了一些关于Text 的描述,它可能是行数。
所以,如果数据超过 3 行,如果它被扩展,我必须显示更多和更少。
<FlatList
style={styles.faltList}
showsVerticalScrollIndicator
data={data}
extraData={this.state}
renderItem={({ item, index }) => (
<View style={styles.flatListCell}>
<Text style={styles.description}>{item.description}</Text>
</View>
</View>
)
}
ItemSeparatorComponent={() => (
<View style={{ height: 10}} />
)}
/>
Run Code Online (Sandbox Code Playgroud)
我找到了react-native-view-more-text库,但我想通过自定义代码来实现它。
注意:我在 Flatlist 中显示该文本。
有什么建议?
小智 14
我是这样试的,希望对你和其他人有帮助!
const postTextContent = (props) => {
const [textShown, setTextShown] = useState(false); //To show ur remaining Text
const [lengthMore,setLengthMore] = useState(false); //to show the "Read more & Less Line"
const toggleNumberOfLines = () => { //To toggle the show text or hide it
setTextShown(!textShown);
}
const onTextLayout = useCallback(e =>{
setLengthMore(e.nativeEvent.lines.length >=4); //to check the text is more than 4 lines or not
// console.log(e.nativeEvent);
},[]);
return (
<View style={styles.mainContainer}>
<Text
onTextLayout={onTextLayout}
numberOfLines={textShown ? undefined : 4}
style={{ lineHeight: 21 }}>{Your Long Text}</Text>
{
lengthMore ? <Text
onPress={toggleNumberOfLines}
style={{ lineHeight: 21, marginTop: 10 }}>{textShown ? 'Read less...' : 'Read more...'}</Text>
:null
}
</View>
)
}
Run Code Online (Sandbox Code Playgroud)
Aur*_*ana -1
import React from 'react';
import PropTypes from 'prop-types';
import AnchorText from '../AnchorText';
import { StyledContainer, RegularText } from './styles';
export default class Description extends React.Component {
constructor(props) {
super(props);
this.state = {
showMore: true,
};
}
onClick = () => {
this.setState({
showMore: false,
});
};
render() {
const { description } = this.props;
const { showMore } = this.state;
if (!description) return null;
return (
<StyledContainer FD={'column'}>
{showMore ? (
<RegularText MT={1}>
{description.slice(0, 150)}{' '}
{description.length > 150 && (
<AnchorText onClick={this.onClick} label=" .. Read more" />
)}{' '}
</RegularText>
) : (
<RegularText MT={1}>{description}</RegularText>
)}
</StyledContainer>
);
}
}
Description.propTypes = {
description: PropTypes.string,
};
Run Code Online (Sandbox Code Playgroud)
看看这个小部件
| 归档时间: |
|
| 查看次数: |
15419 次 |
| 最近记录: |