2010-01-20 7 views
11

J'ai un petit problème avec NSTableView. Quand j'augmente la hauteur d'une rangée dans une table, le texte est aligné en haut de la rangée mais je veux l'aligner verticalement centré! Est-ce que quelqu'un peut me suggérer n'importe quel moyen de le faire?alignement vertical du texte dans la ligne NSTableView

Merci,

Miraaj

Répondre

20

Ceci est une solution simple de code qui montre une sous-classe vous pouvez utiliser pour aligner un milieu TextFieldCell.

l'en-tête

#import <Cocoa/Cocoa.h> 


@interface MiddleAlignedTextFieldCell : NSTextFieldCell { 

} 

@end 

le code

@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 montre une solution alternative qui fonctionne aussi bien.

+1

'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. –

8

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:

enter image description here

et la hauteur du TableViewCell

enter image description here

enter image description here

Merci Bryan pour votre aide.

0

@ iphaaw de réponse mis à jour pour Swift 4 (note I a également ajouté « Cell » à la fin du nom de la classe pour plus de clarté, qui doit également correspondre au nom de la classe dans 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) 
    } 
} 
Questions connexes