ios - SwiftUI 一个文本中心和一个尾随对齐
问题描述
我正在尝试向我制作的自定义图表视图添加一些细节。我目前在 VStack 中有图表和标题。这是完美的工作,意味着标题在图表上方居中。
我现在想在标题下方和视图尾端的图表上方添加一个小文本标题,如下所示
我正在努力理解我需要使用哪些修饰符来正确设置对齐以实现此布局。任何帮助表示赞赏。
代码:
VStack {
// Title Text
Text(title)
.font(.headline)
// The new text I want added (text 2 in the image)
Text(min)
.font(.caption)
// My current attempt but I am not sure this is right
.alignmentGuide(.trailing, computeValue: { _ in 0 })
// Chart
ChartView()
}
解决方案
只需将您的第二个包装Text
在带有垫片的 HStack 中。该垫片将迫使Text
向右。
struct ContentView: View {
var body: some View {
VStack {
// Title Text
Text("Title")
.font(.headline)
HStack {
Spacer() /// forces the Text to the right
// The new text I want added (text 2 in the image)
Text("Text 2")
.font(.caption)
}
// Chart
Rectangle()
.fill(Color.blue)
}
}
}
结果:
推荐阅读
- sql - 当日期为时间戳时,在指定日期之间抓取记录
- verilog - 时钟块层次结构中的信号延迟与波形中的 RTL 层次结构
- c++ - 为什么这个 SQL 查询似乎没有失败,但实际上失败了?
- javascript - Is there is a way to check a word is same to another word?
- python - PGAdmin4 虚拟主机不适用于 Apache2 + WSGI
- r - 如何对齐闪亮的输入框,特别是 selectInput 和 numericInput
- html - 如何使用 CSS 将搜索图标 (
- c# - 泛型和重载多态
- javascript - React Hooks:如何访问自定义属性值?
- c# - 如何解决字符串上可为空的 C# SqlNull 异常?