Jul*_* Re 2 user-interface geometry alignment textview swift
我尝试在文本视图周围放置一个圆形框架,但我无法正确对齐它,也看不出问题出在哪里。如下图所示,文本有时会向左偏移,而应该居中。关于如何解决它的任何想法?
struct ContentView: View {
let result = getDate();
var body: some View {
VStack {
ScrollView(.horizontal, showsIndicators: false) {
HStack(alignment: .top, spacing: 5) {
ForEach(result, id: \.self) {
day in
Text(day.name!)
.frame(width: 35, height: 35, alignment: .center)
.padding()
.overlay(
Circle()
.size(width: 35, height: 35)
.offset(x: 17.5,y: 17.5)
.scale(1.4)
.stroke(Color.orange, lineWidth: 4)
)
}
}
}
Spacer()
}.background(Color.white)
}
}
Run Code Online (Sandbox Code Playgroud)
圆圈偏移了框架大小的一半,所以它的原点应该在中心。文本也应该在 .frame(width: 35, height: 35, alignment: .center) 中居中。
非常感谢您的帮助!:)
.overlay已经有一个容器的大小,其中有Text一个Circle居中,所以只需要用圆的插入操作而不是移动它,如下所示

Text(day.name!)
.frame(width: 35, height: 35, alignment: .center)
.padding()
.overlay(
Circle()
.stroke(Color.orange, lineWidth: 4)
.padding(6)
)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2422 次 |
| 最近记录: |