objective-c - 自定义 NSTextView 边框问题

标签 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

最佳答案

您需要子类化 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

关于objective-c - 自定义 NSTextView 边框问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13928479/

相关文章:

iphone - UIDatePicker 和一个 UITableView

ios - 如果在通话过程中插入了麦克风,则无法恢复录制

iphone - 从 iOS 中的 iPhone 相机库将图像发布到 Facebook

iphone - 选择 UIPickerView 项目时运行 ibaction

swift - 以编程方式退出终端 Mac 应用程序?

objective-c - 显示 View 列表

c# - 对于绘图应用程序,什么是好的 2D 图形框架?

iphone - 何时取消订阅 UIView 中的 NSNotification

Java applet 仅部分绘制

c++ - 粗细的画线变白,OpenCv iOS 问题