html - 如何对 React Native 中使用的 html 内容使用阅读更多和更少的行为?

标签 html react-native

我正在使用 react-native 渲染 html 包。现在我面临一个问题,我必须在 react native 中实现对 html 内容的阅读更多和更少的功能。 我知道 read more and less package work only for TEXT.So there are any way to reach solution for rendering html content with read more and less behavior

最佳答案

您可以轻松地组合这两个包。您可以使用 react-native-read-more-text 的包来阅读更多/更少,并使用 react-native-view-more-text 来显示任何 html 内容.

例如:

import HTML from 'react-native-render-html';
import ViewMoreText from 'react-native-view-more-text';

const htmlContent = `
                <h1>This HTML snippet is now rendered with native components !</h1>
                <h2>Enjoy a webview-free and blazing fast application</h2>
                <h2>Enjoy, This is a test 1</h2>
                <h2>Enjoy, This is a test 2</h2>
                <h2>Enjoy, This is a test 3</h2>
                <em style="textAlign: center;">Look at how happy this native cat is</em>
            `;

export default class Demo extends Component {
  renderViewMore(onPress){
    return(
      <Text onPress={onPress}>View more</Text>
    )
  }
  renderViewLess(onPress){
    return(
      <Text onPress={onPress}>View less</Text>
    )
  }
  render() {
    return ( 
    <ScrollView style = {{flex: 1}}>

     <ViewMoreText
      numberOfLines={3}
      renderViewMore={this.renderViewMore}
      renderViewLess={this.renderViewLess}
      textStyle={{textAlign: 'center'}}
      >
        HTML html = {htmlContent}
        imagesMaxWidth = {Dimensions.get('window').width}
        />
     </ViewMoreText>
    </ScrollView>
    );
  }
}

如果使用 numberOfLines 属性显示较少的文本,您可以轻松地给出行数,目前是 3 行。

这里是包的链接:

https://www.npmjs.com/package/react-native-view-more-text

https://github.com/archriss/react-native-render-html

我希望我能帮上忙:)

关于html - 如何对 React Native 中使用的 html 内容使用阅读更多和更少的行为?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54668412/

相关文章:

ios - React Native 在设备 iOS 上运行崩溃

android - 未定义不是一个对象(评估 StatusBarManager.HEIGHT)react-native 0.55.4

javascript - 使用 Canvas 上下文变量定义位置的怪异行为,导致 Canvas 渲染卡住时,当标签不活动时

javascript - 通过检查 Span 文本将 CSS Class 添加到 Div

html - 按钮在 Firefox 中对齐,但在 Chrome 中不对齐

reactjs - 如何使用数据库条目在 React Native 中渲染样式?

android - Expo DocumentPicker MIME 过滤器不适用于 JSON

html - 我如何在不使用 "Float"的情况下并排获取图像?

javascript - html 图像 src 调用 javaScript 变量

ios - KeyboardAvoidingView 在 IOS 上无法按预期工作