简体   繁体   English

React Native - 文本被截断太多

[英]React Native - text gets truncated too much

I have the following component我有以下组件

const InputLabel => (props: InputLabelProps) {
    return (
        <Animated.View
            style={[
                StyleSheet.absoluteFill,
                {
                    justifyContent: 'flex-start',
                    flexDirection: 'row',
                    backgroundColor: 'red',
                    alignItems: 'center',
                },
            ]}
        >
            <Animated.Text
                onLayout={onLayoutAnimatedText}
                numberOfLines={1}
                ellipsizeMode="tail"
                textBreakStrategy={'highQuality'}
                style={[
                    animatedLabelContainerStyle,
                    {
                        textAlign: 'right',
                        fontSize: fontSize,
                        color: labelColor,
                        backgroundColor: 'yellow',
                    },
                ]}
            >
                {label}
            </Animated.Text>
        </Animated.View>
    );
}

When I reference it from another component and I set the width of wrapper component below a certain value the text is truncated too much leaving a big gap.当我从另一个组件引用它并将包装器组件的宽度设置为低于某个值时,文本被截断太多,留下很大的差距。

<View style={{ width: '40%', height: 50, borderWidth: 1, backgroundColor: 'yellow' }}>
                <InputLabel
                    fontSize={16}
                    hasOutline={false}
                    label={'label 12345678901234567890123456789012345678901234567890'}
                    // labelAnimatedValue={labelAnimation}
                    labelColor={'black'}
                    labelLayout={labelLayout}
                    OffsetY={2} //container borderWidth
                    placeholderColor={'red'}
                    showPlaceholder={false}
                    onLayoutAnimatedText={handleLayoutAnimatedText}
                />
</View>

The code above with width at 40% produces the below Width at 40%上面宽度为 40% 的代码会生成下面的宽度为 40%的代码

When I set width at 50% or greater the text is truncated how I want it Width at 50%当我将宽度设置为 50% 或更大时,文本会按我想要的方式截断Width at 50%

What I want is:我想要的是:

yellow background to stretch to the end of the red container at all width黄色背景以所有宽度延伸到红色容器的末端

Please note if the label length is not long enough to be truncated the yellow color should only wrap text and not stretch to end of red container请注意,如果 label 的长度不够长而无法截断,则黄色应仅包裹文本,而不会拉伸到红色容器的末尾

Snack: https://snack.expo.dev/@danro/inputlabel零食: https://snack.expo.dev/@danro/inputlabel

Expected result with short label短 label 的预期结果预期的短标签

Expected result with long label长 label 的预期结果预期的长标签

Did you try to use it with flex ?您是否尝试将它与flex一起使用? I would try to give flex:1 .我会尝试给flex:1

You can either add flex: 1 or width:"100%" to the text..您可以在文本中添加 flex: 1 或 width:"100%" ..


edit :编辑

in InputLabel.tsx remove flexDirection: row and alignItems: center from Animated ViewInputLabel.tsx中删除 flexDirection: row 和 alignItems: center 从动画视图

Try:尝试:

<Text
     {...props}
     numberOfLines={1}
     ellipsizeMode={'head' | 'middle' | 'tail' | 'clip'}
     onPress={props.onPress}
     style={style.textStyle}>
     Some long long long text
  </Text>

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

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