如何在 React Native for Android 中调整字体大小以适应视图?

新手上路,请多包涵

如何在本机反应中使文本的字体大小在视图内自动更改?

我有不同长度的文本,其中一些不适合视图,因此减小了字体大小。我检查了文档并找到 了这个,但它仅适用于 iOS,我需要它用于 Android。

它是否适用于其他组件,如按钮和可触摸不透明度?

原文由 Taha 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 553
1 个回答

稍微改进的 Jeffy Lee 代码对我有用

 const AdjustLabel = ({
  fontSize, text, style, numberOfLines
}) => {
  const [currentFont, setCurrentFont] = useState(fontSize);

  return (
    <Text
      numberOfLines={ numberOfLines }
      adjustsFontSizeToFit
      style={ [style, { fontSize: currentFont }] }
      onTextLayout={ (e) => {
        const { lines } = e.nativeEvent;
        if (lines.length > numberOfLines) {
          setCurrentFont(currentFont - 1);
        }
      } }
    >
      { text }
    </Text>
  );
};

原文由 Yurii H 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题