2010-01-20 4 views
11

Ho un piccolo problema con NSTableView. Quando sto aumentando l'altezza di una riga nella tabella, il testo al suo interno è allineato in cima alla riga, ma io voglio allinearlo verticalmente centrato!allineamento verticale del testo nella riga NSTableView

Qualcuno può suggerirmi un modo per farlo ??

Grazie,

Miraaj

risposta

20

Questa è una soluzione semplice codice che mostra una sottoclasse è possibile utilizzare per Allinea al centro un TextFieldCell.

nell'intestazione

#import <Cocoa/Cocoa.h> 


@interface MiddleAlignedTextFieldCell : NSTextFieldCell { 

} 

@end 

il codice

@implementation MiddleAlignedTextFieldCell 

- (NSRect)titleRectForBounds:(NSRect)theRect { 
    NSRect titleFrame = [super titleRectForBounds:theRect]; 
    NSSize titleSize = [[self attributedStringValue] size]; 
    titleFrame.origin.y = theRect.origin.y - .5 + (theRect.size.height - titleSize.height)/2.0; 
    return titleFrame; 
} 

- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView { 
    NSRect titleRect = [self titleRectForBounds:cellFrame]; 
    [[self attributedStringValue] drawInRect:titleRect]; 
} 

@end 

This blog entry mostra una soluzione alternativa che funziona anche bene.

+1

'dimensioni 'assume una larghezza infinita - non tiene conto del ritorno a capo. Suggerisco 'boundingRectForSize: options:', con la dimensione da 'cellFrame', invece. –

8

ecco la versione Swift dell'edificio codice a risposta di cui sopra:

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) 
    } 
} 

Poi ho impostato l'altezza della tableView heightOfRow nella NSTableView:

func tableView(tableView: NSTableView, heightOfRow row: Int) -> CGFloat 
{ 
    return 30 
} 

impostare la classe del NSTextFieldCell da VerticallyCenteredTextField:

enter image description here

e l'altezza del TableViewCell

enter image description here

enter image description here

Grazie Bryan per il vostro aiuto.

0

@ di iphaaw risposta aggiornata per Swift 4 (nota che ho anche aggiunto "cella" a fine del nome della classe per chiarezza, che deve anche corrispondere al nome della classe in Interface Builder):

import Foundation 
import Cocoa 

class VerticallyCenteredTextFieldCell : NSTextFieldCell { 
    override func titleRect(forBounds theRect: NSRect) -> NSRect { 
     var titleFrame = super.titleRect(forBounds: theRect) 
     let titleSize = self.attributedStringValue.size 
     titleFrame.origin.y = theRect.origin.y - 1.0 + (theRect.size.height - titleSize().height)/2.0 
     return titleFrame 
    } 

    override func drawInterior(withFrame cellFrame: NSRect, in controlView: NSView) { 
     let titleRect = self.titleRect(forBounds: cellFrame) 
     self.attributedStringValue.draw(in: titleRect) 
    } 
}