2023-09-06

import React, { useRef, useState, useEffect } from 'react';
import {Text, View, ScrollView, Dimensions, TouchableOpacity} from 'react-native';

const windowWidth = Dimensions.get('window').width;

let sentences = [["Groundwater is the word used to describe water that saturates the ground, filling all the available spaces.", "By far the most abundant type of groundwater is meteoric water; this is the groundwater that circulates as part of the water cycle. Ordinary meteoric water is water that has soaked into the ground from the surface, from precipitation (rain and snow) and from lakes and streams.", "There it remains, sometimes for long periods, before emerging at the surface again. ","At first thought it seems incredible that there can be enough space in the \"solid\" ground underfoot to hold all this water."], ["The necessary space is there, however, in many forms.", "The commonest spaces are those among the particles-sand grains and tiny pebbles-of loose, unconsolidated sand and gravel. Beds of this material, out of sight beneath the soil, are common.", "They are found wherever fast rivers carrying loads of coarse sediment once flowed.", "For example, as the great ice sheets that covered North America during the last ice age steadily melted away, huge volumes of water flowed from them."]];

export default function App() {
  const left = 12;
  const right=16;
  const textWidth = windowWidth - left-right;
  const [pageDatas, setPageDatas] = useState([]);
  const [isLoadData, setIsLoadData] = useState(true);
  const datasRef = useRef(([].concat(sentences)).map(i=>i.map(a=>a.split(" "))));
  useEffect(() => {
    const dealWords = ([].concat(sentences)).map(i=>i.map(a=>a.split(" ")));
    setTimeout(() => {
      var datas = [];
      datasRef.current.map((sections, a) => {
        var dataSections = [];
        var leading = 0;
        sections.map((items, b)=>{
          var dataItems = [];
          var text = "";
          items.map((i, index) => {
            if (i + leading > textWidth) {
              dataItems.push(text);
              leading = i;
              text = " "+dealWords[a][b][index];
            } else {
              text += (" "+dealWords[a][b][index]);
              leading += i;
            }
            if (index == items.length - 1) {
              dataItems.push(text);
            } 
          });
          dataSections.push(dataItems);
        });
        datas.push(dataSections);
      })
      setPageDatas(datas);
    }, 1000);  
  }, []);

  useEffect(() => {
    if (pageDatas.length > 0) {
      setIsLoadData(false);
    }
  }, [pageDatas]);

  if (isLoadData) {
    return sentences.map((item, i) => <View>
          {
            item.map((texts, index) => texts.split(" ").map((text, b) => <MeasureTextExample 
            text={" "+ text} fontSize={16} callBack={(w)=>{
              datasRef.current[i][index][b] = w
          }}/>))})
        </View>)
       
  }
  return (
    <View
      style={{
        flex: 1,
      }}>
      <ScrollView style={{width: windowWidth, paddingLeft: left, paddingRight: right}}>
      <View style={{}}>
        {pageDatas.map((item, i) => <View style={{}}>
          <View style={{height: 20}} key={i}/>
          <View style={{flexDirection: 'row', flexWrap: 'wrap'}}>
            {item.map((texts, index) => texts.map((text, b) =>
              <TouchableOpacity>
                <Text 
                  key={'a'+index}
                  style={{lineHeight: 26, color: '#333', fontWeight: index == 2 ? 700: 400, fontSize: index == 2 ? 15: 16}}>
                {text}</Text>
              </TouchableOpacity>))}
          </View>
        </View>)}
       
      </View>
      </ScrollView>
    </View>
  );
}



function MeasureTextExample({text, fontSize=14, fontWeight=400, callBack}) {
  const onTextLayout = event => {
    callBack(event.nativeEvent.layout.width);
  };

  return (
    <View>
      <Text
        onLayout={onTextLayout}
        style={{ position: 'absolute', opacity: 0, fontSize, fontWeight }}
      >
        {text}
      </Text>
    </View>
  );
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 一 ??【原文摘錄】 阿德勒想要幫我們掙脫的第一個束縛:來自過去;第二個束縛:來自人際關(guān)系;第三個束縛:來自未來。 ...
    碧霄詩情_4522閱讀 175評論 0 0
  • 無味。 在以前的更文中,記得總是不由自主的講著一些什么似是而非的人生道理。 這種系列的文字,想必還是讓人覺得很是無...
    Sunny06e閱讀 174評論 0 14
  • 我們已經(jīng)知道,資本是按時間順序通過生產(chǎn)領(lǐng)域和流通領(lǐng)域兩個階段完成運動的。資本在生產(chǎn)領(lǐng)域停留的時間是它的生產(chǎn)時間,資...
    杜文碩閱讀 209評論 0 0
  • 其實每個人都自顧不暇,哪有那么多時間來去關(guān)心別人,所以收不到別人的關(guān)注也很正常,所以就更要珍惜別人對自己的好。 如...
    樓前的樹閱讀 57評論 0 0
  • 存貨視頻沒有了,建議一次性拍個7條,但是還是美食視頻更重要啊,昨天有個人拍的好努力的,其實需要幫忙的話和旁邊人說,...
    林拉米閱讀 80評論 0 1

友情鏈接更多精彩內(nèi)容