简体   繁体   English

如何在 Nativescript 中限制聊天气泡(标签)的宽度?

[英]How to constraint the width of the chat bubble(Label) in Nativescript?

I'm setting up a chat app, and the chat bubble should not exceed the certain width (like around 80% of the screen).我正在设置一个聊天应用程序,聊天气泡不应超过一定的宽度(如屏幕的 80% 左右)。

I read from the other post, they all mentioned to wrap the Label with Layout, it works charm in Android but not iOS.我从另一篇文章中读到,他们都提到用布局包装标签,它在 Android 中很有魅力,但在 iOS 中却没有。

...
<ScrollView row="1" background="#fafafa" ref='scrollView'>
    <FlexboxLayout flexDirection="column" justifyContent="flex-end">
         <GridLayout v-for="chat in processedChats" :key="chat.timestamp" rows='*' backgroundColor="#fafafa" >
             <FlexboxLayout row='0' v-if="chat.speaker == me" flexDirection="row" justifyContent="flex-end">
                 <Label :text="chat.content" :textWrap="true" padding="10" margin="5 5 5 10" fontSize="16" background="#222222" color="#ffffff" borderRadius="15" />
                 <Label text="" width="40%" />
             </FlexboxLayout>


             <FlexboxLayout row='0' v-else flexDirection="row" justifyContent="flex-start">
                 <Label :text="chat.content" :textWrap="true" padding="10" margin="5 5 5 10" fontSize="16" background="#f1f0f0" color="#484848" borderRadius="15" />
                 <Label text="" width="40%" />
             </FlexboxLayout>
         </GridLayout>
    </FlexboxLayout>
</ScrollView>
...

Here is the weird layout result.这是奇怪的布局结果。

Well, I had this problem before.嗯,我以前遇到过这个问题。 Try this尝试这个

<ScrollView row="1" background="#fafafa" ref='scrollView'>
    <FlexboxLayout flexDirection="column" justifyContent="flex-end">
         <GridLayout v-for="chat in processedChats" :key="chat.timestamp" rows='*' backgroundColor="#fafafa" >

             <FlexboxLayout row='0' v-if="chat.speaker == me" flexDirection="row" justifyContent="flex-end" @loaded="resize">
                 <Label :text="chat.content" :textWrap="true" padding="10" margin="5 5 5 10" fontSize="16" background="#222222" color="#ffffff" borderRadius="15" />
                 <Label text="" width="40%" />
             </FlexboxLayout>


             <FlexboxLayout row='0' v-else flexDirection="row" justifyContent="flex-start" @loaded="resize">
                 <Label :text="chat.content" :textWrap="true" padding="10" margin="5 5 5 10" fontSize="16" background="#f1f0f0" color="#484848" borderRadius="15" />
                 <Label text="" width="40%" />
             </FlexboxLayout>

         </GridLayout>
    </FlexboxLayout>
</ScrollView>
...

methods: {

    resize(args) {
        setTimeout(() => {
            if(args.object.getActualSize().width > screen.mainScreen.widthDIPs*0.6) {
               args.object.width = '100%'
            }
        }, 50)
    }
}
...

The code above will auto resize the chat bubble one it exceeds the 60% of the screen width, you may calibrate to the ratio you want.上面的代码会自动调整聊天气泡的大小,超过屏幕宽度的 60%,你可以校准到你想要的比例。

Hope this help!希望这有帮助!

You could use a Label wrapped in a StackLayout with a fixed width and set it to wrap when it hits the StackLayout 's width.您可以使用一个Label包裹在一个具有固定宽度的StackLayout ,并在它达到StackLayout的宽度时将其设置为换行。

<ScrollView row="1" background="#fafafa" ref='scrollView'>
  <StackLayout orientation="vertical">
    <StackLayout v-for="chat in processedChats" :horizontalAlignment="chat.speaker == me ? 'left' : 'right'" width="80%" orientation="horizontal">
      <Label :text="chat.content" textWrap="true" :backgroundColor="chat.speaker == me ? '#222222' : '#f1f0f0'" margin="5 10" padding="5 10" borderRadius="15"></Label>
    </StackLayout>
  </StackLayout>
</GridLayout>

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

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