React-Native WebView 測量網頁高度

React-Native(后面簡稱RN)在展示某些靜態(tài)也頁面的時候茁瘦,可能需要使用WebView涝焙, WebView可以請求一個網頁地址,也可以異步請求HTML文本距潘。一般情況下我們要得到網頁的寬高炼列,傳回給RN以便準確設置WebView展示寬高

原文地址(轉載需注明出處

大致步驟

  1. 使用WebView的injectedJavaScript屬性注入JS代碼,進行測量網頁的寬高
  2. 設置WebView的寬高

PS:是不是很簡單绽昼?哈哈哈~

先來介紹下injectedJavaScript

The injectedJavaScript is a custom prop of the React native Webview component. You can pass any JavaScript code ( as string ) to this prop, and React native will inject this JavaScript code into the Webview. The injected JavaScript will get executed once the Webview is finished loading.轉自(可能需要科學上網)

injectedJavaScript屬性讓注入的代碼在WebView loaded時候運行一次,并只運行一次须蜗,除非你重新加載一次webView硅确。

這個屬性常用的用兩種場景:

修改HTML部分屬性,比如標簽的顏色明肮、字體大小什么的


const HTMLTEXT = `<p id="myContent" style="text-align: center;"><span style="font-family: 宋體, SimSun; color: rgb(247, 150, 70); font-size: 36px;"><strong>大佬讓測試商品詳情頁樣式啊菱农。。</strong></span></p><p style="text-align: center;"><span style="font-family: 宋體, SimSun; color: rgb(75, 172, 198);"><strong>這個怎么說呢柿估。循未。</strong></span></p><p style="text-align: center;"><span style="font-family: 宋體, SimSun; color: rgb(128, 100, 162);"><strong>樣式還是需要解析的啊。秫舌。</strong></span></p><p style="text-align: center;"><span style="font-family: 宋體, SimSun; color: rgb(155, 187, 89);"><strong>如果亂了會被運營打的啊的妖。。<img width="100%" src="http://upload-images.jianshu.io/upload_images/6652326-afa4c73c7fca32ff.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" title="1505131159299088793.jpeg" alt="photo-1457365050282-c53d772ef8b2.jpeg"/></strong></span></p><p style="text-align: center;"><span style="font-family: 宋體, SimSun; color: rgb(192, 80, 77);"><strong>運營也會死的很慘啊足陨。嫂粟。</strong></span></p><p style="text-align: center;"><span style="font-family: 宋體, SimSun; color: rgb(79, 129, 189);"><strong>所以讓我們試一下吧。墨缘。</strong></span></p><p style="text-align: center;"><span style="font-family: 宋體, SimSun; color: rgb(31, 73, 125);"><strong>如果還是不行星虹。零抬。</strong></span></p><p style="text-align: center;"><span style="font-family: 宋體, SimSun; color: rgb(146, 205, 220);"><strong>就拿開發(fā)祭天。宽涌。</strong></span></p><p style="text-align: center;"><span style="font-family: 宋體, SimSun; color: rgb(178, 162, 199);"><strong>不然平夜。。</strong></span></p><p style="text-align: center;"><span style="font-family: 宋體, SimSun; color: rgb(217, 150, 148);"><strong>還能怎么樣呢卸亮。忽妒。</strong></span></p><p style="text-align: center;"><span style="font-family: 宋體, SimSun; color: rgb(155, 187, 89);"><strong><img width="100%" src="http://a.36krcnd.com/nil_class/277e5abe-2f40-41a5-b465-173a551c8d63.jpg" title="1504250661662096451.png" alt="首頁banner2.png"/></strong></span></p><p><span style="font-family: 宋體, SimSun; color: rgb(155, 187, 89);"></span></p><p style="white-space: normal; text-align: center;"><span style="font-family: 宋體, SimSun; color: rgb(227, 108, 9);"><strong>再來一遍!5樟肌锰扶!</strong></span></p><p style="white-space: normal; text-align: center;"><span style="font-family: 宋體, SimSun; color: rgb(227, 108, 9);"></span></p><p style="white-space: normal; text-align: center;"><span style="font-family: 宋體, SimSun; color: rgb(247, 150, 70);"><strong>大佬讓測試商品詳情頁樣式啊。寝受。</strong></span></p><p style="white-space: normal; text-align: center;"><span style="font-family: 宋體, SimSun; color: rgb(75, 172, 198);"><strong>這個怎么說呢坷牛。。</strong></span></p><p style="white-space: normal; text-align: center;"><span style="font-family: 宋體, SimSun; color: rgb(128, 100, 162);"><strong>樣式還是需要解析的啊很澄。京闰。</strong></span></p><p style="white-space: normal; text-align: center;"><span style="font-family: 宋體, SimSun; color: rgb(155, 187, 89);"><strong>如果亂了會被運營打的啊。甩苛。</strong></span></p><p style="white-space: normal; text-align: center;"><span style="font-family: 宋體, SimSun; color: rgb(192, 80, 77);"><strong>運營也會死的很慘啊蹂楣。。</strong></span></p><p style="white-space: normal; text-align: center;"><span style="font-family: 宋體, SimSun; color: rgb(79, 129, 189);"><strong>所以讓我們試一下吧讯蒲。痊土。</strong></span></p><p style="white-space: normal; text-align: center;"><span style="font-family: 宋體, SimSun; color: rgb(31, 73, 125);"><strong>如果還是不行。墨林。</strong></span></p><p style="white-space: normal; text-align: center;"><span style="font-family: 宋體, SimSun; color: rgb(146, 205, 220);"><strong>就拿開發(fā)祭天赁酝。。</strong></span></p><p style="white-space: normal; text-align: center;"><span style="font-family: 宋體, SimSun; color: rgb(178, 162, 199);"><strong>不然旭等。酌呆。</strong></span></p><p style="white-space: normal; text-align: center;"><span style="font-family: 宋體, SimSun; color: rgb(217, 150, 148);"><strong>還能怎么樣呢。搔耕。</strong></span></p><p style="white-space: normal; text-align: center;"><span style="font-family: 宋體, SimSun; color: rgb(155, 187, 89);"></span><br/></p><p style="white-space: normal; text-align: center;"><span style="font-family: 宋體, SimSun; color: rgb(155, 187, 89);"><strong><img width="100%" src="http://upload-images.jianshu.io/upload_images/3510198-c80fd2d06a801e33.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" title="1504250722330056753.png" alt="通欄1.png"/></strong></span></p><p><span style="font-family: 宋體, SimSun; color: rgb(155, 187, 89);"><strong><br/></strong></span><br/></p>`

    // 修改了標簽id為`myContent`的背景顏色
        <WebView
          injectedJavaScript={`
        document.querySelector('#myContent').style.backgroundColor = 'red';
    `}
          style={{
            width: Dimensions.get('window').width,
            height: this.state.height
          }}
          automaticallyAdjustContentInsets
          source={{ html: HTMLTEXT }}
          decelerationRate='normal'
       />
changeProp.png

注入一些方法隙袁,做測量或者其他交互。

這個一般是需要WebView的onMessage方法配合使用

import React, { Component } from 'react'
import {
  WebView,
  Dimensions,
  ScrollView
} from 'react-native'

const BaseScript =
    `
    (function () {
        var height = null;
        function changeHeight() {
          if (document.body.scrollHeight != height) {
            height = document.body.scrollHeight;
            if (window.postMessage) {
              window.postMessage(JSON.stringify({
                type: 'setHeight',
                height: height,
              }))
            }
          }
        }
        setInterval(changeHeight, 100);
    } ())
    `

const HTMLTEXT = `<p id="myContent" style="text-align: center;"><span style="font-family: 宋體, SimSun; color: rgb(247, 150, 70); font-size: 36px;"><strong>大佬讓測試商品詳情頁樣式啊弃榨。菩收。</strong></span></p><p style="text-align: center;"><span style="font-family: 宋體, SimSun; color: rgb(75, 172, 198);"><strong>這個怎么說呢。鲸睛。</strong></span></p><p style="text-align: center;"><span style="font-family: 宋體, SimSun; color: rgb(128, 100, 162);"><strong>樣式還是需要解析的啊坛梁。。</strong></span></p><p style="text-align: center;"><span style="font-family: 宋體, SimSun; color: rgb(155, 187, 89);"><strong>如果亂了會被運營打的啊腊凶。划咐。<img width="100%" src="http://upload-images.jianshu.io/upload_images/6652326-afa4c73c7fca32ff.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" title="1505131159299088793.jpeg" alt="photo-1457365050282-c53d772ef8b2.jpeg"/></strong></span></p><p style="text-align: center;"><span style="font-family: 宋體, SimSun; color: rgb(192, 80, 77);"><strong>運營也會死的很慘啊拴念。。</strong></span></p><p style="text-align: center;"><span style="font-family: 宋體, SimSun; color: rgb(79, 129, 189);"><strong>所以讓我們試一下吧褐缠。政鼠。</strong></span></p><p style="text-align: center;"><span style="font-family: 宋體, SimSun; color: rgb(31, 73, 125);"><strong>如果還是不行。队魏。</strong></span></p><p style="text-align: center;"><span style="font-family: 宋體, SimSun; color: rgb(146, 205, 220);"><strong>就拿開發(fā)祭天公般。。</strong></span></p><p style="text-align: center;"><span style="font-family: 宋體, SimSun; color: rgb(178, 162, 199);"><strong>不然胡桨。官帘。</strong></span></p><p style="text-align: center;"><span style="font-family: 宋體, SimSun; color: rgb(217, 150, 148);"><strong>還能怎么樣呢。昧谊。</strong></span></p><p style="text-align: center;"><span style="font-family: 宋體, SimSun; color: rgb(155, 187, 89);"><strong><img width="100%" src="http://a.36krcnd.com/nil_class/277e5abe-2f40-41a5-b465-173a551c8d63.jpg" title="1504250661662096451.png" alt="首頁banner2.png"/></strong></span></p><p><span style="font-family: 宋體, SimSun; color: rgb(155, 187, 89);"></span></p><p style="white-space: normal; text-align: center;"><span style="font-family: 宋體, SimSun; color: rgb(227, 108, 9);"><strong>再來一遍9艉纭!呢诬!</strong></span></p><p style="white-space: normal; text-align: center;"><span style="font-family: 宋體, SimSun; color: rgb(227, 108, 9);"></span></p><p style="white-space: normal; text-align: center;"><span style="font-family: 宋體, SimSun; color: rgb(247, 150, 70);"><strong>大佬讓測試商品詳情頁樣式啊涌哲。。</strong></span></p><p style="white-space: normal; text-align: center;"><span style="font-family: 宋體, SimSun; color: rgb(75, 172, 198);"><strong>這個怎么說呢尚镰。阀圾。</strong></span></p><p style="white-space: normal; text-align: center;"><span style="font-family: 宋體, SimSun; color: rgb(128, 100, 162);"><strong>樣式還是需要解析的啊。狗唉。</strong></span></p><p style="white-space: normal; text-align: center;"><span style="font-family: 宋體, SimSun; color: rgb(155, 187, 89);"><strong>如果亂了會被運營打的啊初烘。。</strong></span></p><p style="white-space: normal; text-align: center;"><span style="font-family: 宋體, SimSun; color: rgb(192, 80, 77);"><strong>運營也會死的很慘啊分俯。肾筐。</strong></span></p><p style="white-space: normal; text-align: center;"><span style="font-family: 宋體, SimSun; color: rgb(79, 129, 189);"><strong>所以讓我們試一下吧。澳迫。</strong></span></p><p style="white-space: normal; text-align: center;"><span style="font-family: 宋體, SimSun; color: rgb(31, 73, 125);"><strong>如果還是不行局齿。剧劝。</strong></span></p><p style="white-space: normal; text-align: center;"><span style="font-family: 宋體, SimSun; color: rgb(146, 205, 220);"><strong>就拿開發(fā)祭天橄登。。</strong></span></p><p style="white-space: normal; text-align: center;"><span style="font-family: 宋體, SimSun; color: rgb(178, 162, 199);"><strong>不然讥此。拢锹。</strong></span></p><p style="white-space: normal; text-align: center;"><span style="font-family: 宋體, SimSun; color: rgb(217, 150, 148);"><strong>還能怎么樣呢。萄喳。</strong></span></p><p style="white-space: normal; text-align: center;"><span style="font-family: 宋體, SimSun; color: rgb(155, 187, 89);"></span><br/></p><p style="white-space: normal; text-align: center;"><span style="font-family: 宋體, SimSun; color: rgb(155, 187, 89);"><strong><img width="100%" src="http://upload-images.jianshu.io/upload_images/3510198-c80fd2d06a801e33.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" title="1504250722330056753.png" alt="通欄1.png"/></strong></span></p><p><span style="font-family: 宋體, SimSun; color: rgb(155, 187, 89);"><strong><br/></strong></span><br/></p>`

class RZWebView extends Component {
  constructor (props) {
    super(props)
    this.displayName = 'RZWebView'
    this.state = ({
      height: 0
    })
  }

  /**
   * web端發(fā)送過來的交互消息
   */
  onMessage (event) {
    try {
      const action = JSON.parse(event.nativeEvent.data)
      if (action.type === 'setHeight' && action.height > 0) {
        this.setState({ height: action.height })
      }
    } catch (error) {
      // pass
    }
  }

  render () {
    return (
      <ScrollView>
        <WebView
          injectedJavaScript={BaseScript}
          style={{
            width: Dimensions.get('window').width,
            height: this.state.height
          }}
          automaticallyAdjustContentInsets
          source={{ html: HTMLTEXT }}
          decelerationRate='normal'
          scalesPageToFit
          javaScriptEnabled // 僅限Android平臺卒稳。iOS平臺JavaScript是默認開啟的。
          domStorageEnabled // 適用于安卓
          scrollEnabled={false}
          onMessage={this.onMessage.bind(this)}
        />
      </ScrollView>
    )
  }
}

RZWebView.navigationOptions = {
  headerTitle: 'RZWebView'
}

export default RZWebView

WebView.gif

額外補充:WebView 加載URI

RN中WebView都是原生控件他巨,對網頁的支持也是挺好的充坑。直接代碼了:

import React, { Component } from 'react'
import {
  View,
  WebView,
  Dimensions
} from 'react-native'

class RZWebView extends Component {
  constructor (props) {
    super(props)
    this.displayName = 'RZWebView'
  }

  render () {
    return (
      <View style={{
        width: Dimensions.get('window').width,
        height: Dimensions.get('window').height - 64
      }}>
        <WebView
          automaticallyAdjustContentInsets
          source={{ uri: 'https://rzrobert.github.io/' }}
          decelerationRate='normal'
          scalesPageToFit
          javaScriptEnabled // 僅限Android平臺减江。iOS平臺JavaScript是默認開啟的。
          domStorageEnabled // 適用于安卓
          scrollEnabled
        />
      </View>
    )
  }
}

RZWebView.navigationOptions = {
  headerTitle: 'RZWebView'
}

export default RZWebView

loadUri.png

?著作權歸作者所有,轉載或內容合作請聯系作者
  • 序言:七十年代末捻爷,一起剝皮案震驚了整個濱河市辈灼,隨后出現的幾起案子,更是在濱河造成了極大的恐慌也榄,老刑警劉巖巡莹,帶你破解...
    沈念sama閱讀 221,576評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現場離奇詭異甜紫,居然都是意外死亡降宅,警方通過查閱死者的電腦和手機,發(fā)現死者居然都...
    沈念sama閱讀 94,515評論 3 399
  • 文/潘曉璐 我一進店門囚霸,熙熙樓的掌柜王于貴愁眉苦臉地迎上來腰根,“玉大人,你說我怎么就攤上這事邮辽∵氲瘢” “怎么了?”我有些...
    開封第一講書人閱讀 168,017評論 0 360
  • 文/不壞的土叔 我叫張陵吨述,是天一觀的道長岩睁。 經常有香客問我,道長揣云,這世上最難降的妖魔是什么捕儒? 我笑而不...
    開封第一講書人閱讀 59,626評論 1 296
  • 正文 為了忘掉前任,我火速辦了婚禮邓夕,結果婚禮上刘莹,老公的妹妹穿的比我還像新娘。我一直安慰自己焚刚,他們只是感情好点弯,可當我...
    茶點故事閱讀 68,625評論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著矿咕,像睡著了一般抢肛。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上碳柱,一...
    開封第一講書人閱讀 52,255評論 1 308
  • 那天捡絮,我揣著相機與錄音,去河邊找鬼莲镣。 笑死福稳,一個胖子當著我的面吹牛,可吹牛的內容都是我干的瑞侮。 我是一名探鬼主播的圆,決...
    沈念sama閱讀 40,825評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼鼓拧,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了越妈?” 一聲冷哼從身側響起毁枯,我...
    開封第一講書人閱讀 39,729評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎叮称,沒想到半個月后种玛,有當地人在樹林里發(fā)現了一具尸體,經...
    沈念sama閱讀 46,271評論 1 320
  • 正文 獨居荒郊野嶺守林人離奇死亡瓤檐,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 38,363評論 3 340
  • 正文 我和宋清朗相戀三年赂韵,在試婚紗的時候發(fā)現自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片挠蛉。...
    茶點故事閱讀 40,498評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡祭示,死狀恐怖,靈堂內的尸體忽然破棺而出谴古,到底是詐尸還是另有隱情质涛,我是刑警寧澤,帶...
    沈念sama閱讀 36,183評論 5 350
  • 正文 年R本政府宣布掰担,位于F島的核電站汇陆,受9級特大地震影響,放射性物質發(fā)生泄漏带饱。R本人自食惡果不足惜毡代,卻給世界環(huán)境...
    茶點故事閱讀 41,867評論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望勺疼。 院中可真熱鬧教寂,春花似錦、人聲如沸执庐。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,338評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽轨淌。三九已至迂烁,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間猿诸,已是汗流浹背婚被。 一陣腳步聲響...
    開封第一講書人閱讀 33,458評論 1 272
  • 我被黑心中介騙來泰國打工狡忙, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留梳虽,地道東北人。 一個月前我還...
    沈念sama閱讀 48,906評論 3 376
  • 正文 我出身青樓灾茁,卻偏偏與公主長得像窜觉,于是被迫代替她去往敵國和親谷炸。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,507評論 2 359

推薦閱讀更多精彩內容