-1

I'm trying to create an action that will underline a button when pressed by replacing its current title with an attributed string. However, this keeps resulting in an "unrecognized selector sent to instance" error. I've connected one IBAction to this button, and created an IBOutlet weak variable referring to it. To my knowledge these should not interfere with one another.

Here is my code for the action connected to the button

     @IBAction func voButtonTapped(_ sender: Any) {
          let voString = "vo"
          let attributedvo = NSAttributedString(string: voString, attributes: underlineAttribute)
          vButton.setAttributedTitle(attributedvo, for: .normal)
}        

and this is the error below:

[__SwiftValue set]: unrecognized selector sent to instance 0x600001584510

underlinedAttribute:

let underlineAttribute: [NSAttributedString.Key: Any] = [
    .font: UIFont(name: "Rockwell-Bold", size: 35) as Any,
    .foregroundColor: ColorManager.specialYellow,
    .underlineStyle: NSUnderlineStyle.single.rawValue]

vButton:

    @IBOutlet weak var vButton: UIButton!

ColorManager:

struct ColorManager {
  static let specialYellow = Color("Special Yellow")

}

Alex
  • 1
  • 2
  • Can you show on which line exactly the error is shown? Also, please show all relevant codes, what are `vButton` and `underlineAttribute`? – OOPer May 21 '20 at 02:33
  • I've included the code for both vButton and underlineAttribute, however I cannot figure out which line the error is pointing toward, as I'm unsure of how to read the error and clicking it does not bring me to the line in which the error occurs. Sorry for my ineptitude I usually code on jGrasp and the error messages are easier to read. – Alex May 21 '20 at 02:42
  • Try changing `UIFont(name: "Rockwell-Bold", size: 35) as Any` to `UIFont(name: "Rockwell-Bold", size: 35)!`. (Assuming you have successfully installed Rockwell-Bold into your app.) – OOPer May 21 '20 at 02:44
  • I've tried the change and am still getting the error. Rockwell font is apart of the "English" font collection. Therefore, it should be installed automatically right? Since, the font name is "Rockwell" and I've got it bolded then that would be the proper syntax, correct? As in "Rockwell-Bold"? – Alex May 21 '20 at 03:42
  • Thanks for checking, I do not know what are in _the "English" font collection_, so _successfully installed Rockwell-Bold into your app_ includes the case it is already installed. `ColorManager.specialYellow` might be another possibility, but you have not shown the code. – OOPer May 21 '20 at 03:47
  • I've added the color manager code. Sorry for being such a bother lol I'm just completely confused on what the error is being caused by. – Alex May 21 '20 at 04:14
  • Thanks for adding the code, but `Color` is not a standard UIKit type. You need to pass an instance of `UIColor` to `.foregroundColor`. It is very likely the cause of your issue. – OOPer May 21 '20 at 04:20
  • Yea that was the problem. I commented out the .foreground aspect of the attribute and it worked successfully. Thanks for your help! – Alex May 21 '20 at 16:24
  • Please take some time to write an answer by yourself. – OOPer May 21 '20 at 16:31

1 Answers1

0

The colorManager struct was causing the issue.

I had to change it from

   static let specialYellow = Color("special yellow")

to

   static let specialYellow = UIcolor(red: 0.85, green: 0.68, blue: 0.00, alpha: 1.00)

so essentially I couldn't use my original saved color and had to find a similar one from a hex pallet, then I used an online converter to convert it to a UIcolor

Alex
  • 1
  • 2