WWDC 2022 - SwiftUI(4)

유재경·2022년 6월 25일

iOS New Release

목록 보기
9/12

(※ iOS 16+ 지원)

Line Limit Range for text

줄 글이 길어질 때 폰트에 따라 range를 다르게 설정할 수 있다는 장점이 있어 보인다.

Text("abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd abcd ")
	.lineLimit(3...6) // 3줄에서 최대 6줄까지 허용

Text Animation

이제는 텍스트에도 애니메이션을 줄 수 있다! UX적으로 좀 더 역동적인 표현이 가능해질 것 같다!

@State private var userTap: Bool = false

Text("Tapped")
	.fontWeight(userTap ? .bold : .ultraLight)
	.onTapGesture { 
		withAnimation {
		  userTap.toggle()
		}
	}

Automatic Expanding Text Fields

아, 이거 정말 원하던 기능이었다!!! SwiftUI에서는 TextEditor나 TextField의 높이를 고정적으로 쓸 수 밖에 없어서 라이브러리나 workaround를 사용해야만 했는데, 이 기능이 나와서 개인적으로 참 반가웠다. (iOS16부터 사용할 수 있다는 게 무지하게 큰 단점이지만..)

@State private var info: String = ""

TextField("Enter your bio", text: $info, axis: .vertical) // axis만 추가하면 수직으로 늘어난다!
	.lineLimt(...5) // 5줄까지만 허용하도록 정할 수 있다.

5줄 이상이 넘어가면, 우리가 생각하는 이상적인 형태로 글자가 스크롤되어 올라가진다!

Bold and Italic with Boolean

볼드체와 이탈릭체가 Bool값을 가질 수 있다는 게 좀 신기했는데..무슨 말인고 하니 평소에 .bold(), .italic() 이렇게 폰트값을 주고 있는데 이 안에 파라미터로 Bool값을 넣을 수 있다고 한다. 요건 좀 재미있는 것 같당

 @State private var useBold: Bool = false
 @State private var useItalic: Bool = false

VStack {
	Text("Hello")
	  .bold(useBold)
	  .italic(useItalic)

	Toggle("Use Bold", isOn: $useBold)
	Toggle("Use Italic", isOn: $useItalic)
}

Foreground Color with Animation

유독 이번 SwiftUI 신기술에서 텍스트 관련된 UI 기능이 많은 것 같다고 느껴지는 순간이다.


@State private var useRed: Bool = false

Text("Hello")
	.foregroundColor(useRed ? .red : .black)
	.onTapGesture { 
		withAnimation {
		  useRed.toggle()
		}
	}

Toggle At Once

오 모든 토글을 한 번에 on/off시킬 수 있는 기능이 있다는데, 특히 회원가입 시 '전체 동의' 항목 같은 데 쓰이면 편리할 것 같다.

struct Email: Identifiable {
	var id: String
	var isSubscribed: Bool = false
}

@State private var lists = [
	Email(id: "Monthly Updates", isSubscribed: true),
	Email(id: "NewsFlashes", isSubscribed: true),
	Email(id: "Special Offers", isSubscribed: true),
]

Form {
	Section {
	  ForEach($lists) { $list in
		Toggle(list.id, isOn: $list.isSubscribed)
	  } // 우리가 알고 있는 개별 토글
	}

  Section {
	  ForEach($lists) { $list in
		Toggle(isOn: $list.map(\.isSubscribed)) {
		  Text("Subscribe All") // isOn이 앞으로 가고, Text를 쓸 수 있는 형태이다!
		}
 	  }
	}
}

lists중에 하나라도 toggle off가 되면, Subscribe All 항목이 toggle off 되고 (Subscribe All = list1 && list2 && list3)
Subsribe All 항목이 toggle on되면, 모든 list가 on이 되는 방식이다.

참고

profile
iOS 개발

0개의 댓글