0

I am using IQKeyboardManager with Swift. I have added a cancel button on my datepicker. Everything is working, but I am having trouble with the attributed text for the cancel button not taking effect. What am I doing wrong? Here is a code fragment

cell.field.attributedPlaceholder = NSAttributedString(string: "Cancel",
            attributes: [.foregroundColor: UIColor.black,
               .font: UIFont.boldSystemFont(ofSize: 12)])

cell.field.keyboardToolbar.titleBarButton.setTarget(self,
        action:#selector(datePickerWasCanceled(sender:)))

Here is a sample screen shot of current results -- I would have thought the cancel should be in black and bold.

keyboard cancel button does not have specified attributes

Mc.Stever
  • 782
  • 6
  • 16
  • thanks for the reply. it did not work for me though. so I did some more digging and found a solution that works for me... see below – Mc.Stever Dec 17 '18 at 18:27

2 Answers2

1

Use a UIBarButtonItem instead of the attributedPlaceholder with a flexible space either side so you can position it in the middle of the toolbar.

var items: [UIBarButtonItem] = []

let flexSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
let cancel: UIBarButtonItem = UIBarButtonItem(title: "Cancel", style: .done, target: self, action: #selector(self.datePickerWasCanceled))
cancel.setTitleTextAttributes([
  NSAttributedStringKey.font: UIFont.boldSystemFont(ofSize: 17.0),
  NSAttributedStringKey.foregroundColor: UIColor.blue], for: .normal)

items.append(flexSpace)
items.append(cancel)
items.append(flexSpace)

cell.field.keyboardToolbar.items = items
cell.field.keyboardToolbar.sizeToFit()


@objc func datePickerWasCanceled() {
  cell.field.resignFirstResponder()
}
Cameron Porter
  • 801
  • 6
  • 15
0

cell.field.attributedPlaceholder = NSAttributedString(string: "Cancel") cell.field.keyboardToolbar.titleBarButton.titleFont = UIFont.boldSystemFont(ofSize: 17.0) cell.field.keyboardToolbar.titleBarButton.selectableTitleColor = UIColor.black

cell.field.keyboardToolbar.titleBarButton.setTarget(self, action:#selector(datePickerDidCancel(sender:))

Mc.Stever
  • 782
  • 6
  • 16