Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom insertion point for NSTextView

I'm changing the insertion point size by overriding -(void)drawInsertionPointInRect:(NSRect)aRect color:(NSColor *)aColor turnedOn:(BOOL)flag, But it doesn't handle the first blink (when you move the insertion point, it goes back to normal)

I managed to handle the first blink by overriding the private method - (void)_drawInsertionPointInRect:(NSRect)aRect color:(NSColor *)aColor.

But this is not a solution for me since overriding the private method will result in being decline by App Store. I want the app to be in App Store. I see Apps like iAWriter and Writeroom have a custom insertion point and they are in App store.

Does anyone know how they managed to do this, or a better way rather than overriding the private method?

Thanks.

- (void)_drawInsertionPointInRect:(NSRect)aRect color:(NSColor *)aColor
{
    aRect.size.width = 3.0;
    [aColor set];
    [NSBezierPath fillRect:aRect];
}

- (void)drawInsertionPointInRect:(NSRect)aRect color:(NSColor *)aColor turnedOn:(BOOL)flag
{
    if(flag) {
        aRect.size.width = 3.0;
        [aColor set];
        [NSBezierPath fillRect:aRect];
    }
    else {
        [self setNeedsDisplayInRect:[self visibleRect] avoidAdditionalLayout:NO];
    }
}
like image 430
Jensen Avatar asked Mar 26 '13 21:03

Jensen


1 Answers

The problem is the clipping path that is in effect when drawInsertionPointInRect is called.

- (void)drawInsertionPointInRect:(NSRect)rect color:(NSColor *)color turnedOn:(BOOL)flag {
    rect.size.width = 8.0;
    NSBezierPath * path = [NSBezierPath bezierPathWithRect:rect];
    [path setClip]; // recklessly set the clipping area (testing only!)
    [path fill];
}

Note that the above code will leave artifacts (in my testing drawInsertionPointInRect is not called to clear the insertion point, only to draw it). Use setDrawsBackground:YES for a quick and dirty way to clear the artifacts.

like image 118
Sam Jacobson Avatar answered Nov 30 '22 15:11

Sam Jacobson