ios - 更改按钮标题颜色时出现 11db 错误 EmptyDataSet_Swift)
问题描述
我目前在我的应用程序中使用“EmptyDataSet_Swift”来处理空状态,但我不知道如何正确使用 buttonTitle 函数来更改按钮的颜色。这是它的参考:
/// Asks the data source for the title to be used for the specified button state.
/// The dataset uses a fixed font style by default, if no attributes are set. If you want a different font style, return a attributed string.
func buttonTitle(forEmptyDataSet scrollView: UIScrollView, for state: UIControl.State) -> NSAttributedString?
我能够做的是更改文本的标题,但是当我更改前景色时,我收到一个错误。这是我的代码:
func buttonTitle(forEmptyDataSet scrollView: UIScrollView, for state: UIControl.State) -> NSAttributedString? {
let buttonTitle = "Add a Project"
let buttonTitleAttributes: [NSAttributedString.Key: Any] = [
.font: UIFont(name: "HelveticaNeue-Bold", size: 14),
.foregroundColor: UIColor.green.cgColor
]
let attributedButtonTitle = NSAttributedString(string: buttonTitle, attributes: buttonTitleAttributes)
return attributedButtonTitle
}
EmptyDataSet_Swift:https ://github.com/Xiaoye220/EmptyDataSet-Swift
解决方案
弄清楚了:
func buttonTitle(forEmptyDataSet scrollView: UIScrollView, for state: UIControl.State) -> NSAttributedString? {
var text: String?
var font: UIFont?
var textColor: UIColor?
text = "Add Project";
font = UIFont(name: "HelveticaNeue-Bold", size: 18)
textColor = UIColor(hexColor: state == .normal ? "53cf9f" : "53cf9f" )
if text == nil {
return nil
}
var attributes: [NSAttributedString.Key: Any] = [:]
if font != nil {
attributes[NSAttributedString.Key.font] = font!
}
if textColor != nil {
attributes[NSAttributedString.Key.foregroundColor] = textColor
}
return NSAttributedString.init(string: text!, attributes: attributes)
}
推荐阅读
- jenkins - 在同一硬件上编排一系列几乎相同的构建的最佳方式
- python - 反向字符串省略特殊字符和数字
- reactjs - React Native 升级重复符号问题
- python - 循环遍历函数参数以创建 power point 演示文稿
- gradle - 项目“cocoapods”未与 Gradle 链接
- python - 在这个代码示例中,unittest 计算了哪些测试?
- delphi - 这个 FastMM4 无效指针异常是 Delphi 5 的 FastMM 中的一个错误吗?
- javascript - adob animate JS 无法在画布上绘制
- angular - 主动侧边栏药丸不再有效
- bash - 当其中的程序退出时,关闭从 bash 打开的终结者终端