프로젝트에 Custom Font를 적용해보고자한다.
(a) 프로젝트에 Noto Sans폰트를 적용하기 위해서 다운을 받는다.
(a) 프로젝트에 Fonts폴더를 생성한다.
(b) NotoSans파일을 Fonts폴더 하위로 끌어 넣는다.
(c) 다음과 같이 폰트 파일을 프로젝트에 넣기 위해서 Add to targets로 프로젝트를 타겟으로 설정해줘야한다.
(a) Info.plist파일로 들어가서 Fonts provided by application key를 만들어 준다.
(b) Fonts provided by application를 만들면 하위에 itemN으로 자동으로 key가 만들어진다.
(c) itemN에 key에 대한 value로 추가한 Font파일명을 등록한다.
⛔️ 이때 폰트파일에 확장자까지 추가해줘야 한다.
(a) Util폴더를 생성하여, WDFont.swift
파일 생성
(b) font이름인 WDFont.black
로 접근할 수 있도록 정의한다.
import UIKit
enum WDFont: String {
case Black = "NotoSansKR-Black"
case Bold = "NotoSansKR-Bold"
case Light = "NotoSansKR-Light"
case Medium = "NotoSansKR-Medium"
case Regular = "NotoSansKR-Regular"
case Thin = "NotoSansKR-Thin"
func of(size: CGFloat) -> UIFont {
return UIFont(name: self.rawValue, size: size)!
}
static func black(size: CGFloat) -> UIFont {
return WDFont.Black.of(size: size)
}
static func bold(size: CGFloat) -> UIFont {
return WDFont.Bold.of(size: size)
}
static func light(size: CGFloat) -> UIFont {
return WDFont.Light.of(size: size)
}
static func medium(size: CGFloat) -> UIFont {
return WDFont.Medium.of(size: size)
}
static func regular(size: CGFloat) -> UIFont {
return WDFont.Regular.of(size: size)
}
static func thin(size: CGFloat) -> UIFont {
return WDFont.Thin.of(size: size)
}
}
import UIKit
class ViewController: UIViewController {
let lb: UILabel = {
let lb = UILabel()
lb.text = "Hello World"
lb.textAlignment = .center
lb.adjustsFontSizeToFitWidth = true
lb.font = WDFont.Bold.of(size: 30)
return lb
}()
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
setup()
}
func setup() {
addViews()
setConstraints()
}
func addViews() {
view.addSubview(lb)
}
func setConstraints() {
lb.translatesAutoresizingMaskIntoConstraints = false
lb.widthAnchor.constraint(equalToConstant: 100).isActive = true
lb.heightAnchor.constraint(equalToConstant: 100).isActive = true
lb.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
lb.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
}
}
:)