Skip to content

Instantly share code, notes, and snippets.

@koenbok
Created August 27, 2015 14:16
Show Gist options
  • Save koenbok/a1b8d942977f69ff102b to your computer and use it in GitHub Desktop.
Save koenbok/a1b8d942977f69ff102b to your computer and use it in GitHub Desktop.
# pragma mark Custom caret
- (void)drawInsertionPointInRect:(NSRect)aRect color:(NSColor *)aColor turnedOn:(BOOL)flag {
aRect.size.width = self.caretWidth;
[super drawInsertionPointInRect:aRect color:aColor turnedOn:flag];
}
// This is a hack to get the caret drawing to work. I know, I know.
- (void)setNeedsDisplayInRect:(NSRect)invalidRect {
invalidRect.size.width += self.caretWidth - 1;
[super setNeedsDisplayInRect:invalidRect];
}
@soffes
Copy link

soffes commented Feb 27, 2019

Can confirm this works. I know, I know.

@gongzhang
Copy link

gongzhang commented Aug 28, 2020

Swift version:

override func drawInsertionPoint(in rect: NSRect, color: NSColor, turnedOn flag: Bool) {
    var rect = rect
    rect.size.width = customCaretWidth

    color.set()
    let path = NSBezierPath(roundedRect: rect, xRadius: customCaretWidth / 2, yRadius: customCaretWidth / 2)
    path.fill()
}

override func setNeedsDisplay(_ rect: NSRect, avoidAdditionalLayout flag: Bool) {
    var rect = rect
    rect.size.width += customCaretWidth - 1
    super.setNeedsDisplay(rect, avoidAdditionalLayout: flag)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment