11db error when changing button title color EmptyDataSet_Swift)

Viewed 33

I'm currently using "EmptyDataSet_Swift" in my app to handle empty states, but I can't figure out how to use the buttonTitle Function appropriately in order to change the color of the button. Here's a reference to it:

/// 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?

What I've been able to do is change the title of the text, but when I change the foreground color, I receive an error. Here's my code:

 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

}

and here's a screenshot of the error I receive: enter image description here

EmptyDataSet_Swift: https://github.com/Xiaoye220/EmptyDataSet-Swift

1 Answers

Figured it out:

 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)
}
Related