2
votes

I want to make NSTextView dot border, my drawRect: code is below

- (void)drawRect:(NSRect)dirtyRect
{
    [super drawRect:dirtyRect];

    CGFloat lineDash[2];

    lineDash[0] = 1.0;
    lineDash[1] = 1.0;

    NSBezierPath *path = [NSBezierPath bezierPathWithRect:self.bounds];
    [path setLineDash:lineDash count:2 phase:0.0];
    [path stroke];
}

I alse want to give some margin between text and border, my code is below

[textView setTextContainerInset:NSMakeSize(0, 10.0)];
[textView setString:@"This is a testThis is a testThis is a testThis is a test"];

But the result is that the top border is missing, who knows how to fix this? image

1
Maybe [super drawRect:dirtyRect] is setting up a clipping rect, so I would try [path setClip] just before [path stroke]. - Smilin Brian

1 Answers

3
votes

You need to subclass NSScrollView instead of NSTextView. And then you will have a good performance. It can be done like this:

NSScrollView subclass:

-(void)tile {
    id contentView = [self contentView];
    [super tile];
    [contentView setFrame:NSInsetRect([contentView frame], 1.0, 1.0)];
}

-(void)drawRect:(NSRect)dirtyRect {
    CGFloat lineDash[2];

    lineDash[0] = 1.5;
    lineDash[1] = 1.5;

    NSBezierPath *path = [NSBezierPath bezierPathWithRect:self.bounds];
    [path setLineDash:lineDash count:2 phase:0.0];
    [path setLineWidth:2];
    [path stroke];
}

Result:

Screenshot image