Advertisement
Don_Mag

quick UIFontDescriptor test

Dec 23rd, 2022
1,255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.52 KB | None | 0 0
  1. // quick UIFontDescriptor test
  2.  
  3. class AvenirTestVC: UIViewController {
  4.    
  5.     override func viewDidLoad() {
  6.         super.viewDidLoad()
  7.        
  8.         var avFamilyName: String = ""
  9.  
  10.         // let's make sure we have the Avenir Next font family
  11.         for family in UIFont.familyNames {
  12.             if family ==  "Avenir Next" {
  13.                 avFamilyName = family
  14.                 break
  15.             }
  16.         }
  17.        
  18.         if avFamilyName.isEmpty {
  19.             fatalError("Avenir Next font family not found!!!")
  20.         }
  21.        
  22.         let stackView = UIStackView()
  23.         stackView.axis = .vertical
  24.         stackView.spacing = 8
  25.        
  26.         stackView.translatesAutoresizingMaskIntoConstraints = false
  27.         view.addSubview(stackView)
  28.        
  29.         let g = view.safeAreaLayoutGuide
  30.        
  31.         NSLayoutConstraint.activate([
  32.            
  33.             stackView.topAnchor.constraint(equalTo: g.topAnchor, constant: 40.0),
  34.             stackView.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 16.0),
  35.             stackView.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -16.0),
  36.            
  37.         ])
  38.        
  39.         let weights: [UIFont.Weight] = [
  40.             .light, .regular, .medium, .semibold, .bold, .heavy,
  41.         ]
  42.        
  43.         weights.forEach { w in
  44.             let thisDescriptor = UIFontDescriptor(fontAttributes: [
  45.                 UIFontDescriptor.AttributeName.family: avFamilyName,
  46.                 UIFontDescriptor.AttributeName.traits: [
  47.                     UIFontDescriptor.TraitKey.weight: w
  48.                 ]
  49.             ])
  50.             let fnt: UIFont = UIFont(descriptor: thisDescriptor, size: 28)
  51.             let label = UILabel()
  52.             label.font = fnt
  53.             label.text = fnt.fontName
  54.             label.textAlignment = .center
  55.             stackView.addArrangedSubview(label)
  56.         }
  57.        
  58.     }
  59.    
  60. }
  61.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement