首页 > 解决方案 > 当html字符串已经有宽度时如何设置imagesMaxWidth

问题描述

我在开发 React Native 应用程序时使用 react-native-render-html 将字符串转换为 html 元素。<img>我从后端通过 RESTful API 收到了字符串,并且标签中已经设置了宽度和高度:

<img class="aligncenter" src="https://www.allfin.com/u/cms/www/201811/13142949sf02.jpg" width="600" height="408" />

但我希望将图像调整为窗口的最大宽度,所以我使用:

imagesMaxWidth={Dimensions.get('window').width}

整个片段如下:

  <ScrollView style={styles.content}>
    <Text style={styles.title}>{this.props.title}</Text>
    <Text>{this.props.date}</Text>
    <HTML
      html={this.props.content}
      imagesMaxWidth={Dimensions.get('window').width - 40}
    />
  </ScrollView>

但是图像无法调整到窗口的最大宽度。

那么我该如何设置呢?

谢谢

标签: cssimagereact-nativereact-native-render-html

解决方案


使用ignoredStylesprop 忽略原始图片的宽度和高度。用于ignoredStyles={['height', 'width']}解决问题。


推荐阅读