Voici la version Swift du bâtiment de code sur la réponse ci-dessus:
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)
}
}
Puis je régler la hauteur du tableView heightOfRow dans le NSTableView:
func tableView(tableView: NSTableView, heightOfRow row: Int) -> CGFloat
{
return 30
}
Définir la classe de NSTextFieldCell à VerticallyCenteredTextField:
et la hauteur du TableViewCell
Merci Bryan pour votre aide.
'taille 'suppose une largeur infinie - il ne tient pas compte du retour à la ligne. Je suggère 'boundingRectForSize: options:', avec la taille de 'cellFrame', à la place. –