예시 코드로 살펴보는 Swift UI
그리고 이걸 JS/CSS로 바꾸어보는
ios
import SwiftUI
struct ContentView: View {
var body: some View {
Text("Turtle Rock")
.font(.title)
}
}
#Preview {
ContentView()
}
some
은 불투명한 타입
View
는 protocol
이다. View
를 반환형으로 지정하려면 some 이 들어가야 한다. 어떤 구조체가 ui요소로 사용될 수 있다는 것을 보장하는 방법으로 View protocol
을 구현함으로써 보장한다.
불투명
한 타입이라는 건
안에 뭐가 있는지 확실히 말해줄 수는 없지만, 어쨋든 프로토콜
을 준수하는 게 들어가 있어. 라고 말하면서 구체적인 타입을 알려주지 않아도 되는 것이다.
때문에 구체적인 형은 내부에서 지정되며, 외부에서는 어떤 형의 어떤 데이터들이 들어가 있는지에 대해서 정확히 모두 파악할 수 없다.
때문에 피곤한 세부형 명시
를 피하고 싶기 때문에 Swift UI에서는 some view
를 사용하였다.
import SwiftUI
struct ContentView: View {
var body: some View {
VStack(alignment: .leading) {
Text("Turtle Rock")
.font(.title)
HStack {
Text("Joshua Tree National Park")
.font(.subheadline)
Spacer()
Text("California")
.font(.subheadline)
}
}
.padding()
}
}
#Preview {
ContentView()
}
space-evenly
나 space-around
와 비슷한 개념..?이를 js/css로 표현하면
<main style="height: 100vh;width: 100vw; display:flex; flex-flow: column nowrap; align-items : flex-start;">
<div>
<span>Turtle Rock</span>
</div>
<div style="width:100%; display:flex; flex-flow : row nowrap; justify-content : space-evenly;">
<p>Joshua Tree National Park</p>
<p>California</p>
</div>
</main>
이다. 위의 swift코드와 같이 동작한다.
import SwiftUI
struct CircleImage: View {
var body: some View {
Image("turtlerock")
.clipShape(Circle())
.overlay {
Circle().stroke(.white, lineWidth: 4)
}
.shadow(radius: 7)
}
}
#Preview {
CircleImage()
}
import SwiftUI
struct ContentView: View {
var body: some View {
VStack {
MapView()
.frame(height: 300)
CircleImage()
.offset(y: -130)
.padding(.bottom, -130)
VStack(alignment: .leading) {
Text("Turtle Rock")
.font(.title)
HStack {
Text("Joshua Tree National Park")
Spacer()
Text("California")
}
.font(.subheadline)
.foregroundStyle(.secondary)
Divider()
Text("About Turtle Rock")
.font(.title2)
Text("Descriptive text goes here.")
}
.padding()
Spacer()
}
}
}
#Preview {
ContentView()
}