ios - 在iOS中触摸时如何模糊自定义按钮的标题,如系统UIButton的标题?
问题描述
我有以下自定义按钮:
class GreenButton: UIButton {
override init(frame: CGRect) {
super.init(frame: frame)
setup()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}
private func setup() {
backgroundColor = .green
layer.cornerRadius = 4
setTitleColor(.white, for: .normal)
titleLabel?.font = .systemFont(ofSize: 22, weight: .bold)
}
}
但我希望它的标题在触摸时模糊,就像系统 UIButton 的行为一样。如果我这样声明我的按钮GreenButton(type: .system)
,它的标题会模糊,但字体不会改变。如果我将它声明为GreenButton()
,它的字体是可以的,但它的标题并不模糊。如何解决问题?
解决方案
为突出显示的状态设置不同的颜色:
private func setup() {
backgroundColor = .green
layer.cornerRadius = 4
// set title normal color
setTitleColor(.white, for: .normal)
// set title highlighted color
setTitleColor(.gray, for: .highlighted)
titleLabel?.font = .systemFont(ofSize: 22, weight: .bold)
}
推荐阅读
- android - 删除字符时过滤器不更新列表
- java - java.util.stream.Collectors.toMap() 的值函数返回常量值
- java - 如何使用 Date today = new Date() 将日期时间从 java 设置为 sql;
- javascript - 尝试通过 paypal api 创建付款时如何解决响应:{type: "cors"}
- java - 按对象属性分组
- reactjs - 反向代理到静态网站不起作用
- python - 围绕 Flask SqlAlchemy 查询简化一对 if-else 语句
- c# - 在 Visual Studio 2019 中的 Xamarin 中捕获 Xaml 错误的最佳方法是什么?
- jsf - 如何在 p:calendar 中将滑块/选择器初始值设置为“值”?
- c# - SqlDataReader - 使用所有匹配项填充子列表