Возможно, вам следует прочитать документацию для NSCell
.В нем говорится, что вы должны нарисовать границу в функции draw(withFrame:in)
, и вы должны вызвать drawInterior(withFrame:in:)
, если вы переопределите draw(withFrame:in)
.Кроме того, вы должны переопределить cellSize
и вернуть соответствующий размер, который учитывает вашу новую границу.Я обновил пример до полного решения.Создан пример проекта на Github
/**
Creates an custom border, that is just a line underneath the NSTextField.
*/
class CustomBorderTextFieldCell: NSTextFieldCell {
// How thick should the border be
let borderThickness: CGFloat = 3
// Add extra height, to accomodate the underlined border, as the minimum required size for the NSTextField
override var cellSize: NSSize {
let originalSize = super.cellSize
return NSSize(width: originalSize.width, height: originalSize.height + borderThickness)
}
// Render the custom border for the NSTextField
override func draw(withFrame cellFrame: NSRect, in controlView: NSView) {
// Area that covers the NSTextField itself. That is the total height minus our custom border size.
let interiorFrame = NSRect(x: 0, y: 0, width: cellFrame.width, height: cellFrame.height - borderThickness)
let path = NSBezierPath()
path.lineWidth = borderThickness
// Line width is at the center of the line.
path.move(to: NSPoint(x: 0, y: cellFrame.height - (borderThickness / 2)))
path.line(to: NSPoint(x: cellFrame.width, y: cellFrame.height - (borderThickness / 2)))
NSColor.black.setStroke()
path.stroke()
// Pass in area minus the border thickness in the height
drawInterior(withFrame: interiorFrame, in: controlView)
}
}
Это результат