SwiftUI (二) 形状 Circle RoundedRectangle Ellipse

14 阅读1分钟

形状

形状专属方法(fill/stroke)必须写在 frame 前面

  • .fill(Color) 实心填充颜色
  • .stroke(Color, lineWidth: 数值) 空心描边

import SwiftUI

struct ContentView: View {
    var body: some View {
        VStack {
            // 形状标签最后设置frame
            // 圆形
            Circle()
                .frame(width: 80, height: 80)
                .foregroundColor(.purple)

            // 矩形 + 圆角
            RoundedRectangle(cornerRadius: 16)
                .stroke(Color.black, lineWidth: 2)  // 描边(空心)
                .frame(width: 120, height: 60)

            // 椭圆
            Ellipse()
                .fill(Color.yellow)  // 填充颜色
                .frame(width: 100, height: 50)

        }

    }
}

#Preview {
    ContentView()
}