개발

[iOS Dev]UIFont.preferredFont(forTextStyle: UIFont.TextStyle)

후추멍멍이 2021. 11. 21. 15:25
728x90
iOS App Development Study - How to add UIFont 
iOS 어플 개발 공부 - UIFont를 사용하여 텍스트 스타일 적용하기 
  • 개발 환경 : MacBook Pro (13-inch, M1, 2020), Monterey
  • XCode : Version 13.1 (13A1030d)
  • 시뮬레이터 : iPhone 11

font1.font = UIFont.preferredFont(forTextStyle: .body)
font2.font = UIFont.preferredFont(forTextStyle: .callout)
font3.font = UIFont.preferredFont(forTextStyle: .caption1)
font4.font = UIFont.preferredFont(forTextStyle: .caption2)
font5.font = UIFont.preferredFont(forTextStyle: .footnote)
font6.font = UIFont.preferredFont(forTextStyle: .headline)
font7.font = UIFont.preferredFont(forTextStyle: .subheadline)
font8.font = UIFont.preferredFont(forTextStyle: .largeTitle)
font9.font = UIFont.preferredFont(forTextStyle: .title1)
font10.font = UIFont.preferredFont(forTextStyle: .title2)
font11.font = UIFont.preferredFont(forTextStyle: .title3)
더보기

전체 코드

import UIKit

class ViewController: UIViewController {
	var font1 = UILabel()
	var font2 = UILabel()
	var font3 = UILabel()
	var font4 = UILabel()
	var font5 = UILabel()
	var font6 = UILabel()
	var font7 = UILabel()
	var font8 = UILabel()
	var font9 = UILabel()
	var font10 = UILabel()
	var font11 = UILabel()
	
	override func viewDidLoad() {
		super.viewDidLoad()
		
		self.view.addSubview(font1)
		self.view.addSubview(font2)
		self.view.addSubview(font3)
		self.view.addSubview(font4)
		self.view.addSubview(font5)
		self.view.addSubview(font6)
		self.view.addSubview(font7)
		self.view.addSubview(font8)
		self.view.addSubview(font9)
		self.view.addSubview(font10)
		self.view.addSubview(font11)
		
		font1.text = "font.style.body"
		font2.text = "font.style.callout"
		font3.text = "font.style.caption1"
		font4.text = "font.style.caption2"
		font5.text = "font.style.footnote"
		font6.text = "font.style.headline"
		font7.text = "font.style.subheadline"
		font8.text = "font.style.largeTitle"
		font9.text = "font.style.title1"
		font10.text = "font.style.title2"
		font11.text = "font.style.title3"
		
		font1.translatesAutoresizingMaskIntoConstraints = false
		font2.translatesAutoresizingMaskIntoConstraints = false
		font3.translatesAutoresizingMaskIntoConstraints = false
		font4.translatesAutoresizingMaskIntoConstraints = false
		font5.translatesAutoresizingMaskIntoConstraints = false
		font6.translatesAutoresizingMaskIntoConstraints = false
		font7.translatesAutoresizingMaskIntoConstraints = false
		font8.translatesAutoresizingMaskIntoConstraints = false
		font9.translatesAutoresizingMaskIntoConstraints = false
		font10.translatesAutoresizingMaskIntoConstraints = false
		font11.translatesAutoresizingMaskIntoConstraints = false
		
		font1.bottomAnchor.constraint(equalTo: font2.topAnchor, constant: -20).isActive = true
		font2.bottomAnchor.constraint(equalTo: font3.topAnchor, constant: -20).isActive = true
		font3.bottomAnchor.constraint(equalTo: font4.topAnchor, constant: -20).isActive = true
		font4.bottomAnchor.constraint(equalTo: font5.topAnchor, constant: -20).isActive = true
		font5.bottomAnchor.constraint(equalTo: font6.topAnchor, constant: -20).isActive = true
		font6.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
		font7.topAnchor.constraint(equalTo: font6.bottomAnchor, constant: 20).isActive = true
		font8.topAnchor.constraint(equalTo: font7.bottomAnchor, constant: 20).isActive = true
		font9.topAnchor.constraint(equalTo: font8.bottomAnchor, constant: 20).isActive = true
		font10.topAnchor.constraint(equalTo: font9.bottomAnchor, constant: 20).isActive = true
		font11.topAnchor.constraint(equalTo: font10.bottomAnchor, constant: 20).isActive = true
		
		font1.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 20).isActive = true
		font2.leftAnchor.constraint(equalTo: font1.leftAnchor).isActive = true
		font3.leftAnchor.constraint(equalTo: font1.leftAnchor).isActive = true
		font4.leftAnchor.constraint(equalTo: font1.leftAnchor).isActive = true
		font5.leftAnchor.constraint(equalTo: font1.leftAnchor).isActive = true
		font6.leftAnchor.constraint(equalTo: font1.leftAnchor).isActive = true
		font7.leftAnchor.constraint(equalTo: font1.leftAnchor).isActive = true
		font8.leftAnchor.constraint(equalTo: font1.leftAnchor).isActive = true
		font9.leftAnchor.constraint(equalTo: font1.leftAnchor).isActive = true
		font10.leftAnchor.constraint(equalTo: font1.leftAnchor).isActive = true
		font11.leftAnchor.constraint(equalTo: font1.leftAnchor).isActive = true

		font1.font = UIFont.preferredFont(forTextStyle: .body)
		font2.font = UIFont.preferredFont(forTextStyle: .callout)
		font3.font = UIFont.preferredFont(forTextStyle: .caption1)
		font4.font = UIFont.preferredFont(forTextStyle: .caption2)
		font5.font = UIFont.preferredFont(forTextStyle: .footnote)
		font6.font = UIFont.preferredFont(forTextStyle: .headline)
		font7.font = UIFont.preferredFont(forTextStyle: .subheadline)
		font8.font = UIFont.preferredFont(forTextStyle: .largeTitle)
		font9.font = UIFont.preferredFont(forTextStyle: .title1)
		font10.font = UIFont.preferredFont(forTextStyle: .title2)
		font11.font = UIFont.preferredFont(forTextStyle: .title3)
	}
}
728x90