SwiftUI:如何实现垂直文本的左上对齐?

huangapple go评论67阅读模式
英文:

SwiftUI: How to achieve top left alignment for vertical text?

问题

如何使垂直文本对齐?SwiftUI:如何实现垂直文本的左上对齐?。一切都像我想要的一样,但我希望文本像这张图片一样对齐在左上角。SwiftUI:如何实现垂直文本的左上对齐?。 代码:

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 ?SwiftUI:如何实现垂直文本的左上对齐?. Everything is just like I want but I would like the text to align on the top left just like this image.SwiftUI:如何实现垂直文本的左上对齐?. 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)
}

huangapple
  • 本文由 发表于 2023年5月30日 04:15:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76360104.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定