textFiledStyle

2025. 3. 8. 22:43·iOS/SwiftUI
반응형

👋 들어가기 전

이번 포스팅은 다소 낯선 개념이다.

바로 textfielStyle이다. 

 

Style Suffix가 붙은 애들은 앞에 View들의 스타일을 쉽게 변경할 수 있도록
도와주는 프로토콜이다.

 

즉, 우리의 첫번째 Style은 TextField다.


✊기본 TextFieldStyle

먼저 기본적으로 제공해주는 요소를 보자. 

static var supporting types 설명
.automatic DefaultTextFieldStyle 플랫폼에 따른 기본 스타일 
.plane PlanTextFiledStyle 스타일이 없는 형태
.roundBorder RoundedBorderTextFieldStyle 둥근 테두리
.squareBorder SquareBorderTextFieldStyle 사각형 테두리 (iOS 16+)
    VStack {
      TextField("입력하세요", text: $text)
        .textFieldStyle(.automatic)
        .padding()
        .background(.white)

      TextField("입력하세요", text: $text)
        .textFieldStyle(.plain)
        .padding()
        .background(.white)

      TextField("입력하세요", text: $text)
        .textFieldStyle(.roundedBorder)
        .padding()
    }


☝️커스텀 Style 만들기

여기서 핵심은 _body(configuration:) 함수를 구현해야한다.

func _body(configuration: TextField<Self._Label>) -> some View {
	//
}

여가서 configuration 타입을 살펴보면 제네릭에 신기한 형태가 있다 Label이 들어가 있네 ??

TextField를 잠깐 살펴보자.

텍스트를 입력하고 멈춰보니 정적인 Label 형태 같이보인다.

 

그렇다 <Self.Label>은 TextField안에 존재하는 Label을 가르킨다.

우리는 configuration이라는 파라미터를 통해 안에 있는 Label을 꾸미는 것이다.

 

다음과 같이 작성 후

struct CustomTextFieldStyle: TextFieldStyle {
  func _body(configuration: TextField<Self._Label>) -> some View {
    configuration
      .padding(10)
      .background(RoundedRectangle(cornerRadius: 8).stroke(Color.blue, lineWidth: 2))
      .shadow(radius: 2)
  }
}

VStack {
  TextField("입력하세요", text: $text)
    .textFieldStyle(CustomTextFieldStyle())
    .padding()
}

 

짜잔 굉장히 쉽게 적용된다.

 


😀 소감 및 마무리

별도의 뷰를 상황마다 만드는 것보다 이렇게 style 기능을 확장해서 

style 메서드를 통해 갱신하는게 훨씬 편한 것 같다.


출처

 

TextFieldStyle | Apple Developer Documentation

A specification for the appearance and interaction of a text field.

developer.apple.com

 

 

반응형

'iOS > SwiftUI' 카테고리의 다른 글

@Observable 매크로  (0) 2025.07.03
Custom Carousel 만들기  (0) 2025.03.16
.scrollTargetLayout  (0) 2025.03.15
TabView  (0) 2025.03.05
UIViewRepresentable  (0) 2024.10.12
'iOS/SwiftUI' 카테고리의 다른 글
  • Custom Carousel 만들기
  • .scrollTargetLayout
  • TabView
  • UIViewRepresentable
Hamp
Hamp
남들에게 보여주기 부끄러운 잡다한 글을 적어 나가는 자칭 기술 블로그입니다.
  • Hamp
    Hamp의 분리수거함
    Hamp
  • 전체
    오늘
    어제
    • 분류 전체보기 (308) N
      • CS (30)
        • 객체지향 (2)
        • Network (7)
        • OS (6)
        • 자료구조 (1)
        • LiveStreaming (3)
        • 이미지 (1)
        • 잡다한 질문 정리 (0)
        • Hardware (2)
        • 이론 (6)
        • 컴퓨터 그래픽스 (0)
      • Firebase (3)
      • Programing Langauge (38) N
        • swift (32)
        • python (5) N
        • Kotlin (1)
      • iOS (132)
        • UIKit (37)
        • Combine (1)
        • SwiftUI (32)
        • Framework (7)
        • Swift Concurrency (22)
        • Tuist (6)
        • Setting (11)
        • Modularization (1)
        • Instruments (6)
      • PS (59)
        • 프로그래머스 (24)
        • 백준 (13)
        • LeetCode (19)
        • 알고리즘 (3)
      • Git (18)
        • 명령어 (4)
        • 이론 (2)
        • hooks (1)
        • config (2)
        • action (7)
      • Shell Script (2)
      • Linux (6)
        • 명령어 (5)
      • Spring (15) N
        • 어노테이션 (3) N
        • 튜토리얼 (11)
      • CI-CD (4)
      • Android (0)
        • Jetpack Compose (0)
      • AI (1) N
        • 이론 (1) N
  • 블로그 메뉴

    • 홈
    • 태그
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    dispatch
    AVFoundation
    SwiftUI
    protocol
    property
    IOS
    Spring
    투포인터
    boostcamp
    GIT
    lifecycle
    Tuist
    dp
    UIKit
    프로그래머스
    concurrency
    dfs
    백준
    CS
    Swift
  • 최근 댓글

  • 최근 글

  • 반응형
  • hELLO· Designed By정상우.v4.10.0
Hamp
textFiledStyle
상단으로

티스토리툴바