애플의 Document 참조
SwiftUI에 Corner Radius를 걸어주는 방법은 크게 세가지 정도가 있다. 우선 기본적인 것은 애플의 Document에서 그대로 가져왔다.
cornerRadius(_:antialiased:) | Apple Developer Documentation
Clips this view to its bounding frame, with the specified corner radius.
developer.apple.com
애플 Document 코드 .cornerRadius
import SwiftUI
struct ContentView: View {
var body: some View {
VStack {
Text("Rounded Corners")
.frame(width: 175, height: 75)
.foregroundColor(Color.white)
.background(Color.black)
.cornerRadius(25)
}
}
}
애플의 경우는 .frame
으로 Text
를 감싸주고 .cornerRadius
를 통해 Rounded Corners를 표현해줬다. 아래와 같은 두가지 방법도 있다!
background에서 RoundedRectangle활용하기
.background
에서 in
을 넣어준다면 RoundedRectange
모양으로 만들어 줄 수 있다.
import SwiftUI
struct ContentView: View {
var body: some View {
VStack {
Text("Rounded Corners")
.frame(width: 175, height: 75)
.foregroundColor(Color.white)
.background(Color.black, in : RoundedRectangle(cornerRadius: 25))
}
}
}
clipeShape활용하기
이것은 네모난 프레임을 RoundedRectange
모양으로 잘라주었다.
import SwiftUI
struct ContentView: View {
var body: some View {
VStack {
Text("Rounded Corners")
.frame(width: 175, height: 75)
.foregroundColor(Color.white)
.background(Color.black)
.clipShape(RoundedRectangle(cornerRadius: 25))
}
}
}
이렇게 세가지 방법 정도로 구사할 수 있다.
추가 발견
이럴수가 Smooth round corner를 구사할 수 있다. 이 약간의 차이점이 보이시나요...? Rounded Rectangle
의 style
에 .continuous
를 걸어주면 된다!
import SwiftUI
struct ContentView: View {
var body: some View {
VStack {
Text("Rounded Corners")
.frame(width: 175, height: 75)
.foregroundColor(Color.white)
.background(Color.black, in : RoundedRectangle(cornerRadius: 25))
Text("Smooth Corners")
.frame(width: 175, height: 75)
.foregroundColor(Color.white)
.background(Color.black, in : RoundedRectangle(cornerRadius: 25, style: .continuous))
}
}
}
아래와 같이 cornerSize
도 상세하게 정해볼 수 도 있다.
import SwiftUI
struct ContentView: View {
var body: some View {
VStack {
Text("Rounded Corners")
.frame(width: 175, height: 75)
.foregroundColor(Color.white)
.background(Color.black, in : RoundedRectangle(cornerRadius: 25))
Text("Smooth Corners")
.frame(width: 175, height: 75)
.foregroundColor(Color.white)
.background(Color.black, in : RoundedRectangle(cornerSize: CGSize(width: 25, height: 15), style: .continuous))
}
}
}
'SwiftUI > SwiftUI 사용' 카테고리의 다른 글
[SwiftUI] 커스텀 버튼 만들기 (0) | 2023.05.18 |
---|---|
[SwiftUI] 커스텀 ColorSet 을 Asset 에 추가하기 (0) | 2023.05.02 |
[SwiftUI] Claymorphism CardView 구현 (0) | 2023.04.28 |
[SwiftUI] 원하는 곳만 Corner Radius를 걸어주는 방법 (0) | 2023.04.23 |
[SwiftUI] Button style (0) | 2023.04.20 |