我有一个简单的FlatList和TextInput的本地应用程序。当我搜索任何文本时,它会在列表中突出显示,如下所示:
现在我设置了"numberOfLines = 3“(根据我们的要求),现在看起来像这样:
它的要求是看起来像下面这样。(请不要在意它的my WhatsApp屏幕截图)它会在这3行中用"...“显示突出显示的文本。

我使用以下代码显示数据
<Text numberOfLines={3} style={[subTitle,{fontSize:normalizeFontSize(14),lineHeight:normalizeLineHeight(14)}]}>
{getHighlightedText(alert)}
</Text>突出显示功能:
getHighlightedText = text => {
//search text user inputs
const {value} = this.props;
if (value == "" || value == null || value == 0) {
return <Text> {text} </Text>
} else {
// split the search value
const words = value.split(/\s+/g).filter(word => word.length);
// join if search value has more than 1 word
const pattern = words.join('|');
const re = new RegExp(pattern, 'gi')
const children = [];
let before, highlighted, match, pos = 0;
// match using RegExp with my text
const matches = text.match(re);
if (matches != null) {
// loop all the matches
for (match of matches) {
match = re.exec(text)
if (pos < match.index) {
// before has all the text before the word that has to highlighted
before = text.substring(pos, match.index);
if (before.length) {
children.push(before)
}
}
highlighted = <Text style={{backgroundColor: 'coral'}}>{match[0]} </Text>
// text is highlighted
children.push(highlighted);
pos = match.index + match[0].length;
}
}
if (pos < text.length) {
// text after the highlighted part
const last = text.substring(pos);
children.push(last);
}
// children array will have the entire text
return <Text>{children} </Text>
}
}这方面需要React Native gurus的帮助。请好心,我是新来的React Native :)
发布于 2020-11-16 00:14:18
React Native的<Text>在嵌套的<Text>对象中有一个带有numberOfLines的known issue。
这可能就是导致您问题的原因:)
https://stackoverflow.com/questions/61632011
复制相似问题