You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何创建自定义类实现iOS自定义字体的动态使用(含字形图标)

Hey there! Let's break down how to dynamically switch your custom font styles, adjust sizes, and use your glyph-mapped icons smoothly—since you already have the fonts added to your project, most of the heavy lifting is done.

Implement Dynamic Custom Font Switching & Size Adjustment

Step 1: Wrap Font Retrieval Logic

First, let's create a reusable extension for UIFont to avoid repeating UIFont(name:...) calls and keep your font names organized. This makes switching between regular/light styles way cleaner.

extension UIFont {
    // Define an enum for your two font styles
    enum CustomFontStyle {
        case regular
        case light
    }

    // Static method to fetch your custom font with dynamic style and size
    static func customFont(style: CustomFontStyle, size: CGFloat) -> UIFont? {
        let fontName: String
        switch style {
        case .regular:
            fontName = "customregular" // Match the actual internal name of your regular font
        case .light:
            fontName = "customlight" // Match the actual internal name of your light font
        }
        return UIFont(name: fontName, size: size)
    }
}

Step 2: Dynamically Apply Fonts & Sizes

Now you can use this extension anywhere in your code to switch styles or adjust sizes based on your app's logic.

Example 1: Set Dynamic Font for a UILabel

// Replace this with your actual business logic (e.g., user settings, screen size)
let useLightStyle = true
let dynamicFontSize: CGFloat = UIScreen.main.bounds.width > 375 ? 18 : 16

if let customFont = UIFont.customFont(style: useLightStyle ? .light : .regular, size: dynamicFontSize) {
    yourLabel.font = customFont
} else {
    // Fallback to system font if custom font fails to load
    yourLabel.font = UIFont.systemFont(ofSize: dynamicFontSize, weight: useLightStyle ? .light : .regular)
}

Example 2: Display Glyph-Mapped Icons

Since your icons are mapped to font glyphs, just set the corresponding Unicode character as text and apply your custom font:

let iconButton = UIButton(type: .system)
// Replace "\u{E001}" with the actual Unicode for your icon
iconButton.setTitle("\u{E001}", for: .normal)

if let iconFont = UIFont.customFont(style: .regular, size: 24) { // Adjust size as needed
    iconButton.titleLabel?.font = iconFont
}

Step 3: Critical Checks to Avoid Issues

  • Verify Font Setup: Make sure your font files are added to Info.plist under the Fonts provided by application key (include the full filename, like customregular.ttf).
  • Confirm Font Names: Sometimes the font's internal name doesn't match its filename. Run this code to print all available fonts and find the exact name for your custom fonts:
    for family in UIFont.familyNames {
        print("Font Family: \(family)")
        for font in UIFont.fontNames(forFamilyName: family) {
            print("  Font Name: \(font)")
        }
    }
    
    Replace the fontName values in the extension with the names you see in the console.

That's all! This setup lets you dynamically tweak font styles and sizes across your app while keeping your icon glyphs working as expected.

内容的提问来源于stack exchange,提问作者Valentine

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 10:22:36