我想制作 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"];
但是结果是上边框不见了,谁知道怎么解决这个问题?
最佳答案
您需要子类化 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];
}
结果:
关于objective-c - 自定义 NSTextView 边框问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13928479/