【问题标题】:React Native fit Text ComponentReact Native 适配文本组件
【发布时间】:2018-04-21 22:27:29
【问题描述】:

我正在尝试将我的文本放在可用区域内,我的约束是这样的:

  • 文本不应溢出,
  • 文本必须在所有可用视图中放大,
  • 单词不应该被分解成字符并转到下一行句子可以,
  • 当我在父视图中使用 flex 时,文本不应水平扩展视图超过屏幕宽度。

这是我的代码 sn-p:

import React from "react";
import { StyleSheet, Text, View } from "react-native";
import { Font } from "expo";
import Lorem from "./src/helpers/lorem-ipsum-gen";

export default class App extends React.Component {
  constructor() {
    super()
    this.state = {
      fontLoaded: false,
    };
  }
  //theboldfont.ttf
  async componentWillMount() {
    await Font.loadAsync({
      "akzidenz-grotesk-bq-bold": require("./assets/fonts/AkzidenzGrotesk_BQ_Bold.otf"),
    });
    this.setState({ fontLoaded: true });
  }

  render() {
    let loremText = new Lorem().generate(20)

    return (
      <View style={styles.container}>
        <View>
          <View style={{ flex: 0.37 }} > </View>
          <View style={{ flex: 0.25, backgroundColor: "red" }} >
            <View >
              {
                this.state.fontLoaded ? (<Text
                  style={{ fontFamily: "akzidenz-grotesk-bq-bold", fontSize: 50 }}
                  adjustsFontSizeToFit={true}
                >
                  {loremText}
                </Text>) : null
              }

            </View>
          </View>
          <View style={{ flex: 0.38 }} >
            <Text>
              {loremText}
            </Text>
          </View>
        </View>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: "#fff",
    alignItems: "center",
    justifyContent: "center",
  },
});

它的结果:

我为实现目标所做的努力:

  • 我认为它与自定义字体有关并更改为默认字体(无效)
  • 在一些 github 问题中,人们在 text style prop 中写入了 adjustsFontSizeToFit={true}(无效)
  • 我尝试了 allowFontScaling(无效)
  • 删除字体大小(无效)
  • 移除 flex 并设置静态宽度和高度以查看(无效)
  • 以上所有步骤的组合(无效)

【问题讨论】:

标签: javascript react-native fonts flexbox expo


【解决方案1】:

因此,在尝试了文本组件的新功能后,我发现 adjustsFontSizeToFit={true} 在不指定 numberOfLines={number} 的情况下无法正常工作,但它有时会在行尾将我的单词分成字符。从文档来看,它可能在 android 中不可用。所以我仍然需要更好的方法来解决我的问题

【讨论】:

    【解决方案2】:

    注意:此解决方案需要反复试验,并假定容器大小固定。

    您可以使用interpolation 函数根据最长行的长度缩小文本。

    插值函数示例(欢迎拿走我的interpolation function):

    const fontSizeInterpolator = Interpolator.getInterpolator({
        input:  [4,  5,  6,  7,  8,  10, 11, 13, 40],
        output: [45, 45, 32, 28, 28, 22, 19, 15, 10]
    });
    

    这意味着如果最长的行是四个字符长,则将字体大小设置为非常大。如果最长的行是 40 个字符,请将字体大小设置为非常小。通过反复试验,将特定的行长映射到特定的字体大小。

    通过正常逻辑找到文本中最长的一行(这里我每行只有一个单词):

    const words = props.text.split(' ');
    let longestWord = words[0];
    for (const word of words) {
      if (word.length > longestWord.length) {
        longestWord = word;
      }
    }
    

    然后从你的插值器获取字体大小:

    const fontSize = fontSizeInterpolator(longestWord.length);
    

    示例输出:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-24
      • 2020-12-29
      • 2019-04-28
      • 2016-09-05
      • 1970-01-01
      • 2019-01-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多