Don_Mag icon

quick UIFontDescriptor test

Don_Mag | PRO | 12/23/22 12:30:22 PM UTC | 0 ⭐ | 8054 👁️ | Never ⏰ | []
Swift |

1.52 KB

|

None

|

0 👍

/

0 👎

// quick UIFontDescriptor test
 
class AvenirTestVC: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        var avFamilyName: String = ""
 
        // let's make sure we have the Avenir Next font family
        for family in UIFont.familyNames {
            if family ==  "Avenir Next" {
                avFamilyName = family
                break
            }
        }
        
        if avFamilyName.isEmpty {
            fatalError("Avenir Next font family not found!!!")
        }
        
        let stackView = UIStackView()
        stackView.axis = .vertical
        stackView.spacing = 8
        
        stackView.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(stackView)
        
        let g = view.safeAreaLayoutGuide
        
        NSLayoutConstraint.activate([
            
            stackView.topAnchor.constraint(equalTo: g.topAnchor, constant: 40.0),
            stackView.leadingAnchor.constraint(equalTo: g.leadingAnchor, constant: 16.0),
            stackView.trailingAnchor.constraint(equalTo: g.trailingAnchor, constant: -16.0),
            
        ])
        
        let weights: [UIFont.Weight] = [
            .light, .regular, .medium, .semibold, .bold, .heavy,
        ]
        
        weights.forEach { w in
            let thisDescriptor = UIFontDescriptor(fontAttributes: [
                UIFontDescriptor.AttributeName.family: avFamilyName,
                UIFontDescriptor.AttributeName.traits: [
                    UIFontDescriptor.TraitKey.weight: w
                ]
            ])
            let fnt: UIFont = UIFont(descriptor: thisDescriptor, size: 28)
            let label = UILabel()
            label.font = fnt
            label.text = fnt.fontName
            label.textAlignment = .center
            stackView.addArrangedSubview(label)
        }
        
    }
    
}
 

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎