NSTextField 上的自定义边框
Custom Border on NSTextField
我想自定义 NSTextFields 的边框。我四处搜索,我相当确定这需要在 drawInterior(withFrame:in:)
的 NSTextFieldCell 中完成,但我不确定如何。
特别是我只想要一个底部边框,比正常的稍微厚一些。
您可以为文本字段添加背景图片并设置边框样式 none。
您可能应该阅读 NSCell
的文档。它说您必须在 draw(withFrame:in)
函数中绘制边框,并且如果您覆盖 draw(withFrame:in)
,则必须调用 drawInterior(withFrame:in:)
。此外,您必须覆盖 cellSize
和 return 适当的大小,以考虑您的新边框。我将示例更新为完整的解决方案。在 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)
}
}
这是结果
将此代码添加到 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)
}
我想自定义 NSTextFields 的边框。我四处搜索,我相当确定这需要在 drawInterior(withFrame:in:)
的 NSTextFieldCell 中完成,但我不确定如何。
特别是我只想要一个底部边框,比正常的稍微厚一些。
您可以为文本字段添加背景图片并设置边框样式 none。
您可能应该阅读 NSCell
的文档。它说您必须在 draw(withFrame:in)
函数中绘制边框,并且如果您覆盖 draw(withFrame:in)
,则必须调用 drawInterior(withFrame:in:)
。此外,您必须覆盖 cellSize
和 return 适当的大小,以考虑您的新边框。我将示例更新为完整的解决方案。在 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)
}
}
这是结果
将此代码添加到 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)
}