将文本对准屏幕侧面(SwiftUi)
我正在使用 Swiftui 创建一个聊天应用程序。我正在尝试对齐文本,以便它显示在屏幕的右侧,然后来自其他用户的消息将显示在左侧。我尝试过使用 VStack 并使用前导和尾随对齐,我还尝试过仅对齐文本属性。我有一个文本,然后有两个按钮。我希望垃圾桶按钮和铅笔按钮都均匀地排列在右侧。
VStack (alignment: .trailing ) {
HStack {
Text(data.text)
.padding(.bottom, 4)
.padding(.top, 4)
.padding(.leading, 8)
.padding(.trailing, 8)
.foregroundColor(Color.white)
.background(Color(UIColor.systemBlue))
.cornerRadius(20)
Button(action: {
self.showingPopover = true
}) {
Image(systemName: "pencil")
}
Button(action: {
viewModel.deleteData(id: data.id)
}) {
Image(systemName: "trash")
}
}
}
Using Swiftui I'm creating a chat application. I'm trying to align the text so that it appears on the right side of the screen and then messages from the other user will appear on the left. I have tried using VStack and using leading and trailing alignment, I have also tried just aligning the text attribute. I have a text, and then two buttons. I would like the trash can button and pencil button to all evenly line up on the right side.
VStack (alignment: .trailing ) {
HStack {
Text(data.text)
.padding(.bottom, 4)
.padding(.top, 4)
.padding(.leading, 8)
.padding(.trailing, 8)
.foregroundColor(Color.white)
.background(Color(UIColor.systemBlue))
.cornerRadius(20)
Button(action: {
self.showingPopover = true
}) {
Image(systemName: "pencil")
}
Button(action: {
viewModel.deleteData(id: data.id)
}) {
Image(systemName: "trash")
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将框架对齐用于文本就足够了,根据用户的不同
It is enough to use frame alignment for text, depending to user it can be conditionally changed in place, like
您只需要在
hstack
的开头中添加spacer()
:同样,对于回复,将
spacer()
放在末尾在hstack
中,也将文本推向左侧。You just need to add a
Spacer()
in the beginning of theHStack
:Similarly, for the replies put a
Spacer()
at the end of theHStack
, too push the text to the left.