1

I have an textfield, drawn inside a NSButton. The setup is working. The button, the textfield and the value are shown:

enter image description here

Here´s the code how i setup the textfield:

class myButton: NSButton {

    var textfield = NSTextField()

    override func draw(_ dirtyRect: NSRect) {
        super.draw(dirtyRect)

        drawtextfield(text: "hello")
    }

    func drawtextfield(text: String) {

        let dirtyRect = NSRect()
        textfield.frame = CGRect(x: self.frame.origin.x, y: self.frame.origin.y, width: self.frame.width, height: self.frame.height)
        textfield.stringValue = text
        textfield.draw(dirtyRect)
    }
}

now if i call:

myButton.init().drawtextfield(text: "hello2")

I´m getting error: CGContextSetCMYKStrokeColor: invalid context 0x0 How is it possible, that the textfield is drawn by the start of the app and when the function drawtextfield() is called just a second time, i´m getting error? Advice would be much appreciated.

Josch Hazard
  • 323
  • 3
  • 20
  • 3
    Why would you even want to do that when you can set a title to a button? You definitely have a design issue. – El Tomato Jul 18 '17 at 22:54
  • @ElTomato Because I want to fill the whole button with a color to make the gray boarder disappear. See my last [question](https://stackoverflow.com/questions/45175401/how-to-remove-gray-border-from-nsbutton), the UPDATE. Thanks – Josch Hazard Jul 18 '17 at 23:19
  • Don't do this. `draw` should draw, nothing else. – Willeke Jul 19 '17 at 13:46

0 Answers0