Swift NSTextField上的自定义边框
我想自定义NSTextFields的边框。我已经到处搜索过了,非常确定这需要在Swift NSTextField上的自定义边框,swift,cocoa,appkit,Swift,Cocoa,Appkit,我想自定义NSTextFields的边框。我已经到处搜索过了,非常确定这需要在drawInterior(withFrame:in:)中的NSTextFieldCell中完成,但不确定如何完成 具体来说,我只想要一个底部边框,比普通边框稍厚。您可以将文本字段添加到背景图像中,并将边框样式设置为“无” 您可能应该阅读NSCell的文档。它表示必须在draw(withFrame:in)函数中绘制边框,如果覆盖draw(withFrame:in),则必须调用drawInterior(withFrame:
drawInterior(withFrame:in:)
中的NSTextFieldCell中完成,但不确定如何完成
具体来说,我只想要一个底部边框,比普通边框稍厚。您可以将文本字段添加到背景图像中,并将边框样式设置为“无” 您可能应该阅读
NSCell
的文档。它表示必须在draw(withFrame:in)
函数中绘制边框,如果覆盖draw(withFrame:in)
,则必须调用drawInterior(withFrame:in:)
。此外,您必须覆盖cellSize
,并返回考虑新边框的适当大小。我将示例更新为完整的解决方案。在上创建了一个示例项目
这就是结果
将此代码添加到NSTableHeaderCell
override func draw(withFrame cellFrame: NSRect,
in controlView: NSView) {
let path = NSBezierPath()
path.lineWidth = borderWidth
// Line width is at the center of the line.
path.move(to: NSPoint(x: cellFrame.minX, y: cellFrame.minY))
path.line(to: NSPoint(x: cellFrame.maxX, y: cellFrame.minY))
NSColor.black.setStroke()
path.stroke()
self.drawInterior(withFrame: cellFrame, in: controlView)
}
这就是你想要的吗?我已经看到了,但如果你尝试一下并阅读评论,这种方法会导致一系列其他问题。
override func draw(withFrame cellFrame: NSRect,
in controlView: NSView) {
let path = NSBezierPath()
path.lineWidth = borderWidth
// Line width is at the center of the line.
path.move(to: NSPoint(x: cellFrame.minX, y: cellFrame.minY))
path.line(to: NSPoint(x: cellFrame.maxX, y: cellFrame.minY))
NSColor.black.setStroke()
path.stroke()
self.drawInterior(withFrame: cellFrame, in: controlView)
}