【问题标题】:React Native: weird behaviour with TextAlign 'justify' on Android 9React Native:Android 9 上 TextAlign 'justify' 的奇怪行为
【发布时间】:2021-05-07 20:37:10
【问题描述】:

我使用 Android 9 并且 React Native(expo) 的 textAlign 'justify' 应该可以工作,并且在大多数情况下都可以。但是我发现了一个不太容易重现的奇怪行为,但在某些情况下 textAlign 'justify' 引入了不需要的换行符。

在符合预期的代码下方:

import React from 'react';
import { StyleSheet, Text, SafeAreaView, Dimensions } from 'react-native';

export default function App() {
  return (
    <SafeAreaView>
      <Text style={styles.normal_text}>blablablabla super interesting and fun character to play since she can use a lot of different cards for her build, and sometimes adapt her hand to the scenario. However, because she needs careful planning, she’s the </Text>
      <Text style={styles.normal_text}>Lorem Ipsum</Text>
    </SafeAreaView>
  );
}

const styles = StyleSheet.create({
  normal_text: {
    fontSize: 25,
    textAlign: 'justify',
    width:Dimensions.get('window').width - 24,
  },
});

结果:

下面是一个非常相似的代码,但文本中的一个小改动会有所不同:

import React from 'react';
import { StyleSheet, Text, SafeAreaView, Dimensions } from 'react-native';

export default function App() {
  return (
    <SafeAreaView>
      <Text style={styles.normal_text}>blablabla super interesting and fun character to play since she can use a lot of different cards for her build, and sometimes adapt her hand to the scenario. However, because she needs careful planning, she’s the </Text>
      <Text style={styles.normal_text}>Lorem Ipsum</Text>
    </SafeAreaView>
  );
}

const styles = StyleSheet.create({
  normal_text: {
    fontSize: 25,
    textAlign: 'justify',
    width:Dimensions.get('window').width - 24,
  },
});

结果:

我只在第二种情况下删除了一个“bla”,它不再按预期工作。

有什么想法吗?

谢谢你!

【问题讨论】:

    标签: react-native text text-align justify


    【解决方案1】:

    我不能随便评论,所以我做一个回答。

    只需在此处删除字符串末尾的空格即可:

    <Text style={styles.normal_text}>
            blablabla super interesting and fun character to play since she can use
            a lot of different cards for her build, and sometimes adapt her hand to
            the scenario. However, because she needs careful planning, she’s the{"HERE"}
          </Text>
    
    <Text style={styles.normal_text}>
            blablabla super interesting and fun character to play since she can use
            a lot of different cards for her build, and sometimes adapt her hand to
            the scenario. However, because she needs careful planning, she’s the</Text>
    

    【讨论】:

    • 是的,我想它适用于特定情况,但我对其他文本也有同样的问题,而且它们没有以空格结尾,所以一般问题不是来自这里。 Notcie 除此之外,在这两种情况下,最后都有一个空格,并且行为不同。
    • 奇怪的是,当我用我的编辑器自动格式化你的代码时,它会在你的文本末尾添加大括号而不是空格,就像这样:“因为她需要仔细计划,所以她是{ }"
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-30
    • 1970-01-01
    相关资源
    最近更新 更多