我只是想在NSTextView周围画一个单点黑色边框,并且遇到了麻烦。下面的代码是NSTextView的子类:

override init(frame: NSRect, textContainer: NSTextContainer?)
{
    super.init(frame:frame, textContainer: textContainer)

    self.wantsLayer = true
    self.layer?.borderColor = NSColor.black.cgColor
    self.layer?.borderWidth = 1.0
    self.layer?.cornerRadius = 0.0
}


self.wantsLayer = true代码导致NSTextView消失。还有另一种方法可以做到这一点吗?

编辑:这就是我将NSTextView aka PageView添加到其父view的方式:

    let textContainer = NSTextContainer()
    textContainer.widthTracksTextView = true
    textContainer.heightTracksTextView = true

    let pageView = PageView(frame: CGRect(x: 0, y: pagesViewOriginY, width: self.paperSize.width, height: self.paperSize.height), textContainer: textContainer)
    self.addSubview(pageView);

    self.layoutManager.addTextContainer(textContainer);

最佳答案

另一种选择是为文本视图创建一个单独的类,并使用draw()提供框架:

class TextView: NSTextView {
override func draw(_ rect: NSRect) {
  super.draw(rect)
  let border:NSBezierPath = NSBezierPath(rect: bounds)
  let borderColor = NSColor.black
  borderColor.set()
  border.lineWidth = 2.0
  border.stroke()
}
}

08-05 21:54