これを行うには単純で明白な方法はありませんが、AppKitの多くの機能と同様に、サブクラス化する必要があるものを正確に把握することは難しくありません。私はに遭遇し、macOS 10.12に関する私のテストでは、このアプローチはうまくいくようです。つまり、NSTextFieldCell
をサブクラス化して、テキストフレームを変更するためのいくつかのメソッドをオーバーライドするだけです。ここでスウィフト3の変動です:
class CustomTextFieldCell: NSTextFieldCell {
private static let padding = CGSize(width: 4.0, height: 2.0)
override func cellSize(forBounds rect: NSRect) -> NSSize {
var size = super.cellSize(forBounds: rect)
size.height += (CustomTextFieldCell.padding.height * 2)
return size
}
override func titleRect(forBounds rect: NSRect) -> NSRect {
return rect.insetBy(dx: CustomTextFieldCell.padding.width, dy: CustomTextFieldCell.padding.height)
}
override func edit(withFrame rect: NSRect, in controlView: NSView, editor textObj: NSText, delegate: Any?, event: NSEvent?) {
let insetRect = rect.insetBy(dx: CustomTextFieldCell.padding.width, dy: CustomTextFieldCell.padding.height)
super.edit(withFrame: insetRect, in: controlView, editor: textObj, delegate: delegate, event: event)
}
override func select(withFrame rect: NSRect, in controlView: NSView, editor textObj: NSText, delegate: Any?, start selStart: Int, length selLength: Int) {
let insetRect = rect.insetBy(dx: CustomTextFieldCell.padding.width, dy: CustomTextFieldCell.padding.height)
super.select(withFrame: insetRect, in: controlView, editor: textObj, delegate: delegate, start: selStart, length: selLength)
}
override func drawInterior(withFrame cellFrame: NSRect, in controlView: NSView) {
let insetRect = cellFrame.insetBy(dx: CustomTextFieldCell.padding.width, dy: CustomTextFieldCell.padding.height)
super.drawInterior(withFrame: insetRect, in: controlView)
}
}
私は、セルの最小の高さを高めるために、元の-オーバーライドcellSize(forBounds:)
から1つの顕著な変更を行いました。オートレイアウトを使用してセルのサイズを自動的に調整しています。オーバーライドしないと、テキストがクリップされていました。
センターで調整してみませんか? – Do2