Skip to content

Instantly share code, notes, and snippets.

@SatoshiN303
Created October 14, 2023 13:02
Show Gist options
  • Save SatoshiN303/178462957fa6597618fdeddcb22a3f88 to your computer and use it in GitHub Desktop.
Save SatoshiN303/178462957fa6597618fdeddcb22a3f88 to your computer and use it in GitHub Desktop.
UIButton.Configurationを利用するとself.titleLabel.numberOfLinesを設定しても反映されないための対応
convenience init() {
self.init(frame: .zero)
translatesAutoresizingMaskIntoConstraints = false
if #available(iOS 15, *) {
var config: UIButton.Configuration = .filled()
config.titleAlignment = .center
config.titleLineBreakMode = .byWordWrapping
let vPadding: CGFloat = 8.0
let hPadding: CGFloat = 12.0
config.contentInsets = NSDirectionalEdgeInsets(
top: vPadding,
leading: hPadding,
bottom: vPadding,
trailing: hPadding
)
var backgroundConfig: UIBackgroundConfiguration = .clear()
backgroundConfig.backgroundColor = .buttonTertiaryWhite
backgroundConfig.cornerRadius = 10.0
config.background = backgroundConfig
configuration = config
// NOTE: UIButton.Configurationを利用するとself.titleLabel.numberOfLinesを設定しても反映されないための対応
configurationUpdateHandler = { button in
if let titleLabel = button.titleLabel {
DispatchQueue.main.async {
titleLabel.numberOfLines = 2
titleLabel.lineBreakMode = .byTruncatingTail
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment