2012-12-18 74 views
2

我要讓NSTextView點的邊界,我的drawRect:代碼如下自NSTextView邊界問題

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

我ALSE想給文字和邊界之間一些餘量,我的代碼如下

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

但結果是上邊框丟失,誰知道如何解決這個問題? image

+1

也許'[超級的drawRect:dirtyRect]'是建立一個剪裁矩形,所以我會在'[path stroke]'之前嘗試'[path setClip]'。 –

+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

+0

這是一種解決問題的方法,但我沒有將NSTextView放在NSScrollView上,因此在[path stroke]之前使用[path setClip]更好。謝謝! –