2010-01-20 9 views
11

Mam mały problem z NSTableView. Gdy zwiększam wysokość wiersza w tabeli, tekst w nim jest wyrównany w górnej części wiersza, ale chcę wyrównać go pionowo w środku!wyrównanie w pionie tekstu w wierszu NSTableView

Czy ktoś może zaproponować mi sposób, aby to zrobić?

Dzięki,

Miraaj

Odpowiedz

20

Jest to proste rozwiązanie kodu, który pokazuje podklasę można użyć do środkowej wyrównać TextFieldCell.

nagłówek

#import <Cocoa/Cocoa.h> 


@interface MiddleAlignedTextFieldCell : NSTextFieldCell { 

} 

@end 

kod

@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 przedstawia alternatywne rozwiązanie, które również działa dobrze.

+1

'rozmiarze 'przyjmuje nieskończoną szerokość - nie uwzględnia zwijania linii. Zamiast tego sugeruję 'boundingRectForSize: options:', o rozmiarze z 'cellFrame'. –

8

Oto Swift wersja budynku kodu na odpowiedź powyżej:

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

Potem ustawić wysokość Tableview heightOfRow w NSTableView:

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

Ustaw klasę NSTextFieldCell być VerticallyCenteredTextField:

enter image description here

i wysokość TableViewCell

enter image description here

enter image description here

Dzięki Bryan za pomoc.

0

@ iphaaw za odpowiedź aktualizowana Swift 4 (zauważ Dodałem też „komórka” na końcu nazwy klasy dla jasności, co wymaga również, aby dopasować nazwę klasy w konstruktorze Interface):

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) 
    } 
} 
Powiązane problemy