250x250
반응형
Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

개발자 수니

[iOS/Swift] Color Set으로 Custom Color관리하기 (with. SwiftGen) 본문

iOS - Swift

[iOS/Swift] Color Set으로 Custom Color관리하기 (with. SwiftGen)

개발자 수니 2022. 2. 8. 15:16
728x90
반응형

📌  이번 글은

Xcode에서 Color Set을 사용하여 Custom Color를 관리하는 방법입니다.

 

1. Colors.xcassets

Assets Catalog 생성 - Colors.xcassets

저는 Asset Catalog를 생성하여 Color Assets을 별도로 관리하고 있습니다.

(디폴트로 생성된 Assets.xcassets 파일에 관리해도 무관합니다.)

 

Color Set을 추가

 

Any Appearance 선택 > 오른쪽 Inspector 탭 확인

이곳에서 Color에 대한 작업이 이루어질 것입니다.

 

Appearance 선택 

프로젝트에서 Dark/Light 모드 대응 상황에 따라 선택합니다.

저는 다크모드를 사용하지 않을 것이기 때문에, None을 선택하였습니다.

 

Input Method 선택

Color를 설정에 사용할 방식을 선택합니다.

저는 RGB 형식을 사용하기 위해 8-bit (0-255)를 선택하였습니다.

 

이름 변경

Color에 적합하게 이름도 변경해줍니다.

 

Color 생성

이러한 방식으로 필요한 Color들을 생성합니다.

 

2. Swift에서 Color Set 사용 

Color가 필요한 곳에 아래와 같이 Color Set을 호출하여 사용합니다.

let color: UIColor? = UIColor(named: "default")

 

3. SwiftGen과 함께 Color Set 사용 

SwiftGen

저는 SwiftGen을 사용하여 Color Set을 사용합니다.

SwiftGen은 프로젝트 리소스(예: images, localised strings 등)에 대한 Swift Code를 자동으로 생성하여 사용하기에 안전한 유형으로 만드는 도구입니다.

SwiftGen 참고 포스팅

 

[iOS/Swift] SwiftGen 적용하기 (pod 'SwiftGen')

📌  이번 글은 Xcode 프로젝트에 SwiftGen 적용 방법입니다. 이번에 토이 프로젝트에 SwiftGen을 적용하면서, 굉장히 유용한 라이브러리 같아 정리해봅니다. SwiftGen - 프로젝트 리소스(예: images, locali

sunidev.tistory.com

 

swiftgen.yml 설정

## 구성 항목이 모두 공통 입/출력 상위 디렉토리를 사용하는 경우 여기서 해당 디렉토리를 지정할 수 있습니다.
## 그러면 나머지 구성의 모든 입/출력 경로가 이를 기준으로 표현됩니다.
## 이 두 개의 최상위 키는 선택 사항이며 기본값은 "."(구성 파일의 디렉토리)입니다.
input_dir: ${PROJECT_DIR}/${TARGET_NAME}/Resources
output_dir: ${PROJECT_DIR}/${TARGET_NAME}/Resources

## images, colors, ARKit resources 등에 대한 constants를 포함하여 Assets Catalogs에 대한 constants를 생성합니다.
xcassets:
  - inputs: Assets.xcassets
    outputs:
      - templateName: swift5
        output: Assets.swift
        params:
          enumName: Assets
  - inputs: Colors.xcassets
    outputs:
      - templateName: swift5
        output: Colors.swift
        params:
          enumName: Colors
          
## 자세한 내용은 'swiftgen config doc'을 사용하여 GitHub에 대한 전체 문서를 여십시오.
## https://github.com/SwiftGen/SwiftGen/tree/6.5.1/Documentation/

 

코드에서 사용

Color가 필요한 곳에 아래와 같이 호출하여 사용

let color: UIColor = Colors.default.color

 

 

 

 

🙋🏻‍♀️  참고

[iOS - swift] ColorSet 세팅하여 편리하게 사용하는 방법 (RGB, custom color)

728x90
반응형
Comments