I've read in this post that in order to make the configuration of UIButton adjust my custom font's size automatically I need to add implementation to recalculate the font's size inside configurationUpdateHandler
. But how would this look like?
I also read something about matching the font's text style. But at this point I'm just guessing. Here's the code:
let loginButton = UIButton(configuration: config, primaryAction: nil)
loginButton.configurationUpdateHandler = { button in
guard var config = button.configuration else { return }
let traits = button.traitCollection
let baseTitleFont = UIFont.customFont(ofSize: 18, weight: .semibold)
let baseSubtitleFont = UIFont.customFont(ofSize: 18, weight: .regular)
let scaledTitleFont = UIFontMetrics(forTextStyle: .body).scaledFont(for: baseTitleFont, compatibleWith: traits)
let scaledSubtitleFont = UIFontMetrics(forTextStyle: .body).scaledFont(for: baseSubtitleFont, compatibleWith: traits)
config.titleTextAttributesTransformer = UIConfigurationTextAttributesTransformer { incoming in
var outgoing = incoming
outgoing.font = scaledTitleFont
return outgoing
}
config.subtitleTextAttributesTransformer = UIConfigurationTextAttributesTransformer { incoming in
var outgoing = incoming
outgoing.font = baseSubtitleFont
return outgoing
}
button.configuration = config
}
Thanks in advance!