您好,这就是我的 UIButton 的定义和初始化方式:
lazy var addButton: UIButton = {
let button = UIButton(radius: 32, title: "+", font: .poppinsRegular.withSize(40), backgroundColor: .purple)
button.menu = UIMenu(title: "")
button.showsMenuAsPrimaryAction = true
return button
}()
extension UIButton {
convenience init(
radius: CGFloat = 0,
title: String? = nil,
backgroundColor: UIColor? = nil,
foregroundColor: UIColor = .white,
font: UIFont? = nil
) {
var configuration = UIButton.Configuration.filled()
configuration.baseForegroundColor = foregroundColor
configuration.title = title
configuration.background.cornerRadius = radius
configuration.cornerStyle = .fixed
configuration.contentInsets = .zero
configuration.titleTextAttributesTransformer = UIConfigurationTextAttributesTransformer { incoming in
var outgoing = incoming
outgoing.font = font
return outgoing
}
configuration.baseBackgroundColor = backgroundColor
self.init(configuration: configuration)
}
}
尝试一下...
在你的
.titleTextAttributesTransformer
:
configuration.titleTextAttributesTransformer = UIConfigurationTextAttributesTransformer { incoming in
var outgoing = incoming
outgoing.font = font
// add this line
outgoing.foregroundColor = foregroundColor
return outgoing
}
并添加
ConfigurationUpdateHandler
:
let handler: UIButton.ConfigurationUpdateHandler = { button in
var background = UIButton.Configuration.plain().background
background.cornerRadius = 32
background.backgroundColor = .purple
button.configuration?.background = background
}
addButton.configurationUpdateHandler = handler