Created
October 14, 2023 13:02
-
-
Save SatoshiN303/178462957fa6597618fdeddcb22a3f88 to your computer and use it in GitHub Desktop.
UIButton.Configurationを利用するとself.titleLabel.numberOfLinesを設定しても反映されないための対応
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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