daven11
daven11

Reputation: 3025

how do I display a blinking caret in an NSView?

I'm building a custom NSView subclass that displays and edit's text - I've got to the bit to display a blinking caret - I can't find a reference anywhere - could someone point me to the function or a reference? (In windows I'd do ShowCaret - piece of cake). tia.

Upvotes: 0

Views: 749

Answers (2)

Rob Keniger
Rob Keniger

Reputation: 46020

If you really need to do this, then you would have to implement it yourself. Your view should have a boolean ivar to store the current blink state and you'll need to use a repeating timer to do something like this in your timer method:

- (void)updateCaret:(NSTimer*)timer
{
    caretBlinkActive = !caretBlinkActive; //this sets the blink state
    [self setNeedsDisplayInRect:[self caretRect]];
}

You'd need to implement caretRect to return the current caret rectangle.

In your implementation of drawRect: you'd need to optimise drawing so that only the dirty rect is drawn, and you'd use the value of caretBlinkActive to either draw the caret or not.

To create the timer in the first place you'd do something like:

[NSTimer scheduledTimerWithTimeInterval:caretBlinkRate 
                                 target:self 
                               selector:@selector(updateCaret:) 
                               userInfo:nil 
                                repeats:YES];

Upvotes: 1

hwaxxer
hwaxxer

Reputation: 3383

Why not use NSTextView? You'd get this behavior for free.

Upvotes: 0

Related Questions