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

如何在Xib中自定义TextStyle?修改body TextStyle默认配置方法

在Xib中自定义动态Text Style配置的方法

方法一:通过Info.plist定义自定义动态字体样式

你可以在项目的Info.plist中添加自定义的动态文本样式,之后就能在Xib的字体选择器里直接选用该样式,步骤如下:

  1. 打开Info.plist,右键选择「Add Row」,添加UIAppFontDescriptions(类型为Array)
  2. 在这个Array中添加一个Dictionary,包含以下键值对:
    • UITextStyle:String类型,值设为自定义样式名,比如CustomBody
    • UIFontSize:Number类型,值设为30(你的基准字号)
    • UIFontDescriptorName:String类型,值设为系统字体名,比如SFProText-Regular
  3. 返回Xib,选中目标UILabel,在Attributes Inspector的「Font」选项中,选择「Text Styles」→「CustomBody」(你刚定义的样式)
  4. 这个自定义样式会自动跟随系统的字体缩放设置,和原生Text Style的表现一致

方法二:自定义UILabel子类,通过IBInspectable可视化配置

如果需要更灵活的控制,可编写UILabel子类,通过IBInspectable让你在Xib里直接设置基准字号和对应的Text Style:

import UIKit

class DynamicFontLabel: UILabel {
    @IBInspectable var baseFontSize: CGFloat = 17 {
        didSet {
            updateFont()
        }
    }
    
    @IBInspectable var textStyleName: String = "body" {
        didSet {
            updateFont()
        }
    }
    
    private var textStyle: UIFont.TextStyle {
        switch textStyleName.lowercased() {
        case "title1": return .title1
        case "title2": return .title2
        case "title3": return .title3
        case "headline": return .headline
        case "subheadline": return .subheadline
        case "body": return .body
        case "callout": return .callout
        case "footnote": return .footnote
        case "caption1": return .caption1
        case "caption2": return .caption2
        default: return .body
        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        updateFont()
    }
    
    override func prepareForInterfaceBuilder() {
        super.prepareForInterfaceBuilder()
        updateFont()
    }
    
    private func updateFont() {
        let baseFont = UIFont.systemFont(ofSize: baseFontSize)
        font = UIFontMetrics(forTextStyle: textStyle).scaledFont(for: baseFont)
        adjustsFontForContentSizeCategory = true
    }
}

使用步骤:

  • 在Xib中选中UILabel,将Class改为DynamicFontLabel
  • 在Attributes Inspector里就能看到新增的「Base Font Size」和「Text Style Name」属性,直接设置Base Font Size为30、Text Style Name为body即可
  • 该子类会自动处理字体缩放,效果和代码中设置的完全一致

方法三:使用User Defined Runtime Attributes(无需子类)

如果不想编写子类,可通过Runtime Attributes给UILabel设置自定义基准字号,需先给UILabel添加分类:

import UIKit

extension UILabel {
    private struct AssociatedKeys {
        static var baseFontSize = "baseFontSize"
        static var textStyle = "textStyle"
    }
    
    @objc var baseFontSize: CGFloat {
        get {
            return objc_getAssociatedObject(self, &AssociatedKeys.baseFontSize) as? CGFloat ?? 17
        }
        set {
            objc_setAssociatedObject(self, &AssociatedKeys.baseFontSize, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
            updateDynamicFont()
        }
    }
    
    @objc var textStyle: String {
        get {
            return objc_getAssociatedObject(self, &AssociatedKeys.textStyle) as? String ?? "body"
        }
        set {
            objc_setAssociatedObject(self, &AssociatedKeys.textStyle, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
            updateDynamicFont()
        }
    }
    
    private func updateDynamicFont() {
        let style: UIFont.TextStyle
        switch textStyle.lowercased() {
        case "title1": style = .title1
        case "title2": style = .title2
        case "title3": style = .title3
        case "headline": style = .headline
        case "subheadline": style = .subheadline
        case "body": style = .body
        case "callout": style = .callout
        case "footnote": style = .footnote
        case "caption1": style = .caption1
        case "caption2": style = .caption2
        default: style = .body
        }
        let baseFont = UIFont.systemFont(ofSize: baseFontSize)
        font = UIFontMetrics(forTextStyle: style).scaledFont(for: baseFont)
        adjustsFontForContentSizeCategory = true
    }
    
    override open func awakeFromNib() {
        super.awakeFromNib()
        updateDynamicFont()
    }
}

使用步骤:

  • 在Xib中选中UILabel,打开Identity Inspector,找到「User Defined Runtime Attributes」
  • 添加两个属性:
    • Key Path: baseFontSize,Type: Number,Value: 30
    • Key Path: textStyle,Type: String,Value: body
  • 运行后,该Label会自动应用你设置的基准字号和对应的动态字体缩放

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 21:13:26