Swift

Swift - 커스텀 폰트 적용 방법 및 활용 방법

Goniii 2025. 1. 5. 18:38

1. 폰트 다운로드

  • 폰트를 적용하기 위해서는 폰트 파일을 가지고 있어야 함
  • 폰트 파일은 .ttf, .otf 파일이 존재하는데 otf 파일이 애플에서 개발한 저장 방식!
    • .ttf 파일을 사용해도 무관함
  • 폰트 다운로드

 

2. 폰트 추가

  • Xcode에서 프로젝트 내 폰트 폴더를 만들고 폴더 내부에 파일 저장 (드래그 앤 드롭)

 

 

3. Info.plist 설정

  • Info.plist에서 “Fonts Provided by application” 추가
  • 이후, item에 폰트 이름을 넣어주기

 

 

4. 폰트 적용

4-1. 커스텀 폰트로 사용할 enum 타입 정의

public enum CustomFont {
    case SejongGeulggot
    case GanwonEduBold
    case GanwonEduLight
    case KyoboHandWriting
    case PretendardLight
    case PretendardRegular
    case PretendardMedium
    case PretendardBold
    case PretendardSemiBold
    case Inter
}

 

 

4-2. UIFont Extension

extension UIFont {
    class func customFont(font : CustomFont, ofSize fontSize : CGFloat) -> UIFont {
        switch font {
        case .GanwonEduBold:
            return UIFont(name: "GangwonEduAll-OTFBold", size: fontSize) ?? .systemFont(ofSize: fontSize, weight: .bold)
        case .GanwonEduLight:
            return UIFont(name: "GangwonEduAll-OTFLight", size: fontSize) ?? .systemFont(ofSize: fontSize, weight: .light)
        case .KyoboHandWriting:
            return UIFont(name: "KyoboHandwriting2021sjy", size: fontSize) ?? .systemFont(ofSize: fontSize, weight: .regular)
        case .PretendardBold:
            return UIFont(name: "Pretendard-Bold", size: fontSize) ?? .systemFont(ofSize: fontSize, weight: .bold)
        case .PretendardMedium:
            return UIFont(name: "Pretendard-Medium", size: fontSize) ?? .systemFont(ofSize: fontSize, weight: .medium)
        case .PretendardRegular:
            return UIFont(name: "Pretendard-Regular", size: fontSize) ?? .systemFont(ofSize: fontSize, weight: .regular)
        case .PretendardSemiBold:
            return UIFont(name: "Pretendard-SemiBold", size: fontSize) ?? .systemFont(ofSize: fontSize, weight: .semibold)
        case .PretendardLight:
            return UIFont(name: "Pretendard-Light", size: fontSize) ?? .systemFont(ofSize: fontSize, weight: .light)
        case .SejongGeulggot:
            return UIFont(name: "SejongGeulggot", size: fontSize) ?? .systemFont(ofSize: fontSize)
        case .Inter:
            return UIFont(name: "InterVariableItalic-Thin", size: fontSize) ?? .systemFont(ofSize: fontSize)
        }
    }
}

/* Example
  label.font = .customFont(font: .GanwonEduBold, ofSize: 14)
 */

→ Figma로 협업할 경우, 폰트의 rawValue값에 따라 폰트 굵기를 다르게 하므로 rawValue에 맞게 다른 폰트를 설정할 수 있게 커스텀 필요함

 

import UIKit

public enum CustomFont {
    case SejongGeulggot
    case GanwonEdu
    case KyoboHandWriting
    case Pretendard
    case Inter
}

extension UIFont {
    class func customFont(font: CustomFont, ofSize fontSize: CGFloat, rawValue: Float) -> UIFont {
        switch (font, rawValue) {
        case (.GanwonEdu, 600):
            return UIFont(name: "GangwonEduAll-OTFBold", size: fontSize) ?? .systemFont(ofSize: fontSize, weight: .bold)
        case (.GanwonEdu, 300):
            return UIFont(name: "GangwonEduAll-OTFLight", size: fontSize) ?? .systemFont(ofSize: fontSize, weight: .light)
        case (.Pretendard, 600):
            return UIFont(name: "Pretendard-Bold", size: fontSize) ?? .systemFont(ofSize: fontSize, weight: .bold)
        case (.Pretendard, 400):
            return UIFont(name: "Pretendard-Regular", size: fontSize) ?? .systemFont(ofSize: fontSize, weight: .regular)
        case (.Pretendard, 300):
            return UIFont(name: "Pretendard-Light", size: fontSize) ?? .systemFont(ofSize: fontSize, weight: .light)
        case (.KyoboHandWriting, _):
            return UIFont(name: "KyoboHandwriting2021sjy", size: fontSize) ?? .systemFont(ofSize: fontSize, weight: .regular)
        case (.SejongGeulggot, _):
            return UIFont(name: "SejongGeulggot", size: fontSize) ?? .systemFont(ofSize: fontSize, weight: .regular)
        case (.Inter, _):
            return UIFont(name: "InterVariableItalic-Thin", size: fontSize) ?? .systemFont(ofSize: fontSize, weight: .regular)
        default:
            return .systemFont(ofSize: fontSize)
        }
    }
}

/* Example
label.font = .customFont(font: .GanwonEdu, ofSize: 14, rawValue: 600) // Bold 폰트 반환
label.font = .customFont(font: .GanwonEdu, ofSize: 14, rawValue: 300) // Light 폰트 반환
*/

 

 

ETC.

폰트의 실제 이름과 내가 저장한 이름이 다를 수 있으니 확인 필요

return UIFont(name: "InterVariableItalic-Thin", size: fontSize) ?? .systemFont(ofSize: fontSize)

→ 해당 코드에서 name 값에 실제 폰트 이름을 할당해야 제대로 폰트가 적용됨

 

실제 이름 확인 코드

func checkfont () {
	for family in UIFont. familyNames{
		print (family)
		for name in UIFont. fontNames (forFamilyName: family) {
			print (name)
		}
	}
}

 

 

 

 

https://odinios.tistory.com/4

 

[Swift] Custom Font 추가하기

새로운 프로젝트 시작할 때 쯤이면 매번 까먹는 Font 추가하는 방법...😅 안녕하세요. 오딘(Odin)입니다. 몇 번의 프로젝트를 만들어도 Font를 추가할 때마다 놓치고 까먹는 부분이 생겨서 이왕 이

odinios.tistory.com