[swift] Swift FontBlaster를 사용하여 앱의 토스트 메시지에 폰트를 적용하는 방법은?

Swift에서 폰트를 적용하는 것은 간단하지만, 시스템의 기본 폰트 이외의 폰트를 적용하려면 추가적인 작업이 필요합니다. 이때 Swift FontBlaster라는 라이브러리를 사용하면 편리하게 폰트를 적용할 수 있습니다.

FontBlaster를 사용하여 앱의 토스트 메시지에 폰트를 적용하는 방법을 알아보겠습니다.

먼저, 프로젝트에 FontBlaster를 설치해야 합니다. Cocoapods를 사용하는 경우, Podfile에 다음과 같이 추가합니다.

pod 'FontBlaster'

설치 후, 터미널에서 pod install 명령을 실행하여 라이브러리를 프로젝트에 추가합니다.

FontBlaster를 사용하려면, 앱이 시작될 때 모든 폰트를 로드해야 합니다. AppDelegate.swift 파일에 다음 코드를 추가합니다.

import FontBlaster

class AppDelegate: UIResponder, UIApplicationDelegate {

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        
        FontBlaster.blast() // 모든 폰트 로드
        
        return true
    }
}

이제 원하는 View Controller에서 폰트를 적용할 수 있습니다. 토스트 메시지를 나타내는 함수를 작성하고, 해당 함수에서 폰트를 설정합니다.

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var toastLabel: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        showToast(message: "안녕하세요!", fontName: "YourCustomFont")
    }

    func showToast(message: String, fontName: String) {
        toastLabel.text = message
        toastLabel.font = UIFont(name: fontName, size: 14)
        toastLabel.isHidden = false
    }
}

위의 코드에서 YourCustomFont 부분을 원하는 폰트 이름으로 변경하면 됩니다.

이제 앱을 실행하면, 토스트 메시지에 적용한 폰트가 나타나는 것을 확인할 수 있습니다.

FontBlaster를 사용하면 앱 전체에 원하는 폰트를 적용할 수 있으므로, 필요한 곳에서 필요한 폰트를 손쉽게 사용할 수 있습니다.

FontBlaster에 대한 자세한 내용은 GitHub 페이지를 참고하시기 바랍니다.