简体   繁体   English

React Native 中使用文本组件的牢不可破的行

[英]Unbreakable Line in React Native Using Text Component

Is it possible fix this in code?是否可以在代码中解决这个问题? I want that the text 170km/h stay always in the same line.我希望文本170km/h始终保持在同一行。

在此处输入图片说明

I tried reproduce the problem in a html page and this problem doesn't exists, just in react.我尝试在 html 页面中重现该问题,但此问题不存在,只是为了反应。 See:看:

 p { background-color: #0000FF; color: #FFFFFF; padding: 20px } p.first { width: 181px; } p.second { width: 180px; }
 <p class='first'> I want that the text 170km/h stay always in the same line. </p> <p class='second'> I want that the text 170km/h stay always in the same line. </p>

Now my React code!现在我的 React 代码! You can see it here: https://snack.expo.io/ryaIT4ExQ你可以在这里看到: https : //snack.expo.io/ryaIT4ExQ

import React, { Component } from 'react';
import { Text, View, StyleSheet } from 'react-native';
import { Constants } from 'expo';

export default class App extends Component {
  render() {
    return (
      <View style={styles.container}>
        <Text style={styles.paragraph}>
          Hi! I'm a text and I'm inside a Text Component and I want that 170km/h stay always in the same line. How what I need do to make this?
        </Text>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
    paddingTop: Constants.statusBarHeight,
    backgroundColor: '#ecf0f1',
  },
  paragraph: {
    backgroundColor: "red",
    margin: 24,
    fontSize: 18,
    fontWeight: 'bold',
    color: '#ffffff',
  },
});

It seems the nowrap property hasn't been added yet (by the way, Sacha must have ommited the native part here with that span suggestion).似乎还没有添加 nowrap 属性(顺便说一句,Sacha 一定在此跨度建议中省略了本机部分)。 So far the only solution I've found was changing the slash symbol to its HTML entity as shown here .到目前为止,我已经找到了唯一的解决办法是改变斜杠其HTML实体如图所示这里 I'm aware of the fact that it looks a bit different, but well, it works.我知道它看起来有点不同,但它确实有效。 Guess we have to use that until the native JSS works as it works.猜猜我们必须使用它,直到本机 JSS 正常工作为止。

I would put 170km/h in a span and give it white-space: nowrap我会把170km/h放在一个跨度内并给它white-space: nowrap

I want that the text <span style="white-space: nowrap">170km/h</span> stay always in the same line.

https://css-tricks.com/forums/topic/prevent-word-breaks/ https://css-tricks.com/forums/topic/prevent-word-breaks/

只需为文本样式 flexWrap 添加道具:'wrap',

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM