2

I have a custom NSView and I place three NSTextFields into the view programmatically (axes labels for a graph, basically):

var axesTextFields : Array<NSTextField> = [NSTextField(),NSTextField(),NSTextField()]

func addTextFields() {
    axesTextFields[0].stringValue = "x"
    axesTextFields[1].stringValue = "y"
    axesTextFields[2].stringValue = "z"
    for tf in axesTextFields {
        tf.textColor = NSColor.blackColor()
        tf.bezeled         = false
        tf.editable        = false
        tf.drawsBackground = false
        self.addSubview(tf)
    }
}

I update the location of the NSTextFields in the drawRect() function:

override func drawRect(fullRect: NSRect)
{
    ... // drawing lines for the graph
    axesTextFields[0].frame = NSMakeRect(0,axes_location * 3 - 10,20,20)
    axesTextFields[1].frame = NSMakeRect(0,axes_location * 2 - 10,20,20)
    axesTextFields[2].frame = NSMakeRect(0,axes_location - 10,20,20)
}

The view sometimes draws correctly, but many times it looks like this (with the NSTextFields corrupted on the left):

corrupted drawing

When I drag the window to resize it, it gets drawn correctly most of the time:

correct drawing

I'm not sure where I'm going wrong.

Chris Gregg
  • 2,376
  • 16
  • 30

0 Answers0