본문 바로가기

학습 노트

(186)
21. Animation Animation Apple Developer Documentation developer.apple.com struct AnimationView: View { @State private var position = CGPoint.zero var body: some View { VStack { Circle() .foregroundColor(.blue) .frame(width: 50, height: 50) .position(position) .offset(x: 50, y: 50) .animation(.easeIn) Spacer() Button(action: { self.position = self.position == .zero ? CGPoint(x: 300, y: 300) : .zero }, label: {..
20. Image & SFSymbol & AsyncImage Image Apple Developer Documentation developer.apple.com struct ColorImageView: View { var body: some View { Image("sampleBG") } } Image View는 말 그대로 이미지를 표시하는 View다. 정확히는 Labeled Image와 Unlabeled Image가 존재하며, 같은 View를 생성하지만 전달하는 파라미터가 다르다. 보통은 생성자에 Asset에 추가한 이미지의 이름을 전달해 사용한다. 이미지는 실제 크기를 사용하지만 modifier를 사용해 자유롭게 변경할 수 있다. struct ColorImageView: View { var body: some View { Image("sampleBG") .re..
19. Color & Material Color Apple Developer Documentation developer.apple.com struct ColorImageView: View { var body: some View { VStack { Text("This") .foregroundColor(Color(uiColor: .systemOrange)) .fontWeight(.black) Text("is") .foregroundColor(.cyan) .fontWeight(.black) Text("Color") .foregroundColor(Color(red: 0.4, green: 0.2, blue: 0.7)) .fontWeight(.black) } } } Color 그 자체로도 하나의 View의 역할을 하지만, 보통은 다른 View의 Mod..
18. Stepper & Picker & Date Picker & Color Picker Stepper Apple Developer Documentation developer.apple.com struct ControlsView: View { @State private var stepperVal = 0 var body: some View { VStack { Spacer() Image(systemName: "gear") .resizable() .frame(width: CGFloat(stepperVal) * 5, height: CGFloat(stepperVal) * 5, alignment: .center) .scaledToFit() Spacer() Stepper() { Label { Text("value is \(stepperVal) and real gear change to \(stepperV..
17. Toggle & Slider & ProgressView Toggle Apple Developer Documentation developer.apple.com struct ControlsView: View { @State private var toggleStat = true var body: some View { Image(systemName: toggleStat ? "lightbulb.fill" : "lightbulb") Toggle(isOn: $toggleStat) { Text("Switch it!") } } } title과 label로 이름을 설정하고, isOn에 State Variable을 전달해 해당 변수를 true 나 false로 반전시킨다. 기본 스타일은 너비 전체를 채우고, Control이 오른쪽, 높이는 콘텐츠 표시를 위한 최소한으로 지정된다...
16. Button & Link & Menu Button Apple Developer Documentation developer.apple.com 일반적으로 사용하는 파라미터는 아래와 같다 action Button의 동작 label Button의 UI title Button의 이름 role Alert, Context에 Button을 사용하는 경우 특정 역할을 부여 struct ControlsView: View { @State private var text = "" var body: some View { Text(text) Button("test", role: .destructive) { text = "Hello" } } } Link Apple Developer Documentation developer.apple.com struct Controls..
TextField 입력값 제한하기 입력값을 제한할 때는 보통 세 가지를 고려해야 한다. 숫자만 입력한다고 가정 해 보자. 사용자는 숫자키보드(numeric)가 아닌 SW키보드를 사용할 가능성이 있다. 블루투스나 iPad의 스마트 키보드등의 외장 HW키보드를 사용할 가능성이 있다. 복사, 붙여넣기로 값을 입력할 수 있다. struct LabelView: View { @State private var value = "" @State private var input = "" let formatter: NumberFormatter = { let formatter = NumberFormatter() formatter.numberStyle = .decimal return formatter }() var body: some View { Form { ..
14 ~ 15. TextField & TextField Style & TextEditor TextField Apple Developer Documentation developer.apple.com TextField는 한 줄 입력에 최적화된 View이다. struct LabelView: View { @State private var value = "" var body: some View { Form { Text("testing") .padding() TextField("title", text: $value, prompt: Text("prompt1")) .padding() TextField("title", text: $value) .padding() } } } 생성자의 기본 파라미터는 세 개로 아래와 같다. title iOS와 iPadOS에서 placeholder로 사용된다. macOS에서 La..
13. Label & Font Label Apple Developer Documentation developer.apple.com Label은 이미지와 문자열을 표시하기에 적합한 View다. SwiftUI 초기에는 존재하지 않았기 때문에 하위 호환성을 고려해 사용해야 한다. struct LabelView: View { var body: some View { Label("Bulb", systemImage: "lightbulb") } } 기본 형태는 title + systemImage이고, Custom은 title + iCon으로 이 경우엔 Image View와 Text View를 조합해 사용하는 것과 크게 다르지 않다. struct LabelView: View { var body: some View { NavigationView { B..
12. Text Text 문자열을 표시하는 가장 기초적인 View이지만 기능이 꽤나 많은 편에 속한다. Locailzed String Localizable 파일 생성하기 프로젝트 내에 'Strings File'을 생성하고, 이름을 Localizable로 변경한다. 다른 이름을 사용하지 않는 것이 좋다. Localizable 파일 적용하기 프로젝트의 설정에서 Localizations를 찾아 원하는 언어를 추가한다. 이때 우측 상단의 검색을 이용하면 편리하다. Base와 English가 기본으로 적용돼 있다. 앞서 생성한 Localizable.strings 파일을 선택한 뒤 File Inspector에서 'Localize...' 버튼을 누른다. 아무 언어나 선택해 Localize를 누르면 이전에 눌었던 Localize.....