英文:
SwiftUI: How to achieve top left alignment for vertical text?
问题
如何使垂直文本对齐?。一切都像我想要的一样,但我希望文本像这张图片一样对齐在左上角。。 代码:
import SwiftUI
struct LycéesRow: View {
var lycée: Lycée
var body: some View {
ZStack(alignment: .topLeading) {
lycée.image
.resizable()
.aspectRatio(contentMode: .fill)
.frame(width: 365, height: 280)
.clipped()
.cornerRadius(15)
.offset(y: -35)
RoundedRectangle(cornerRadius: 15)
.frame(width: 365, height: 280)
.background(.ultraThinMaterial, in: RoundedRectangle(cornerRadius: 15))
.offset(y: -35)
lycée.image
.resizable()
.aspectRatio(contentMode: .fill)
.frame(width: 355, height: 200)
.clipped()
.cornerRadius(15)
.offset(x: 5)
.offset(y: 39.8)
VStack(spacing: 1) {
Text(lycée.département + ", France")
.font(.system(size: 15, weight: .medium, design: .default))
.foregroundColor(.white)
.opacity(0.5)
Text(lycée.nom)
.font(.system(size: 25, weight: .bold, design: .serif))
.foregroundColor(.white)
.padding([.top, .leading])
Text(lycée.ville)
.font(.system(size: 10, weight: .light, design: .default))
.foregroundColor(.white)
.padding([.top, .leading])
}.offset(y: -25)
}
}
}
struct LycéesRow_Previews: PreviewProvider {
static var previews: some View {
Group {
LycéesRow(lycée: lycées[0])
LycéesRow(lycée: lycées[1])
}
}
}
我尝试过使用内边距(padding)来手动对齐,但这很繁琐,并且只对特定示例(Nevers)有效,对其他学校来说效果不佳,无法在左上角对齐。
英文:
How can I align the vertical text ?. Everything is just like I want but I would like the text to align on the top left just like this image.. The code :
import SwiftUI
struct LycéesRow: View {
var lycée: Lycée
var body: some View {
ZStack(alignment: .topLeading) {
lycée.image
.resizable()
.aspectRatio(contentMode: .fill)
.frame(width: 365, height: 280)
.clipped()
.cornerRadius(15)
.offset(y: -35)
RoundedRectangle(cornerRadius: 15)
.frame(width: 365, height: 280)
.background(.ultraThinMaterial, in: RoundedRectangle(cornerRadius: 15))
.offset(y: -35)
lycée.image
.resizable()
.aspectRatio(contentMode: .fill)
.frame(width: 355, height: 200)
.clipped()
.cornerRadius(15)
.offset(x: 5)
.offset(y: 39.8)
VStack(spacing: 1) {
Text(lycée.département + ", France")
.font(.system(size: 15, weight: .medium, design: .default))
.foregroundColor(.white)
.opacity(0.5)
Text(lycée.nom)
.font(.system(size: 25, weight: .bold, design: .serif))
.foregroundColor(.white)
.padding([.top, .leading])
Text(lycée.ville)
.font(.system(size: 10, weight: .light, design: .default))
.foregroundColor(.white)
.padding([.top, .leading])
}.offset(y: -25)
}
}
}
struct LycéesRow_Previews: PreviewProvider {
static var previews: some View {
Group {
LycéesRow(lycée: lycées[0])
LycéesRow(lycée: lycées[1])
}
}
}
I tried padding, at first I aligned everything manually which was very annoying but it only worked with that specific example (Nevers) and with other schools, it didn't render well at all and wasn't in the top left.
答案1
得分: 1
答案是添加VStack(alignment: .leading, spacing: x)
,使其看起来像这样:
VStack(alignment: .leading, spacing: 1) {
Text(lycée.département + ", France")
.font(.system(size: 15, weight: .medium, design: .default))
.foregroundColor(.white)
.opacity(0.5)
Text(lycée.nom)
.font(.system(size: 25, weight: .bold, design: .serif))
.foregroundColor(.white)
Text(lycée.ville)
.font(.system(size: 13, weight: .light, design: .default))
.foregroundColor(.white)
}
英文:
answer is to add VStack(alignment: .leading, spacing: x)
so it looks like this :
VStack(alignment: .leading, spacing: 1) {
Text(lycée.département + ", France")
.font(.system(size: 15, weight: .medium, design: .default))
.foregroundColor(.white)
.opacity(0.5)
Text(lycée.nom)
.font(.system(size: 25, weight: .bold, design: .serif))
.foregroundColor(.white)
Text(lycée.ville)
.font(.system(size: 13, weight: .light, design: .default))
.foregroundColor(.white)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论