2

我想制作 NSTextView 点边框,我的 drawRect: 代码如下

- (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];
}

我也想在文本和边框之间留出一些边距,我的代码如下

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

但结果是顶部边框丢失,谁知道如何解决这个问题? 图片

4

1 回答 1

3

您需要子类NSScrollView化而不是NSTextView. 然后你会有很好的表现。可以这样做:

NSScrollView 子类:

-(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];
}

结果:

截图图片

于 2012-12-18T21:59:39.877 回答