自定义NSTextView边框问题

时间:2012-12-18 07:45:42

标签: objective-c cocoa drawing nstextview

我想制作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"];

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

1 个答案:

答案 0 :(得分: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];
}

<强>结果:

Screenshot image