Вот Swift версия здания кода на ответ выше:
import Foundation
import Cocoa
class VerticallyCenteredTextField : NSTextFieldCell
{
override func titleRectForBounds(theRect: NSRect) -> NSRect
{
var titleFrame = super.titleRectForBounds(theRect)
var titleSize = self.attributedStringValue.size
titleFrame.origin.y = theRect.origin.y - 1.0 + (theRect.size.height - titleSize.height)/2.0
return titleFrame
}
override func drawInteriorWithFrame(cellFrame: NSRect, inView controlView: NSView)
{
var titleRect = self.titleRectForBounds(cellFrame)
self.attributedStringValue.drawInRect(titleRect)
}
}
Затем я установил высоту Tableview heightOfRow в NSTableView:
func tableView(tableView: NSTableView, heightOfRow row: Int) -> CGFloat
{
return 30
}
Установите класс NSTextFieldCell быть VerticallyCenteredTextField:
и высота TableViewCell
Спасибо Bryan за вашу помощь.
'размер 'принимает бесконечную ширину - он не учитывает перенос строк. Я предлагаю 'boundingRectForSize: options:', с размером от 'cellFrame', вместо этого. –