2010-06-28 32 views
0

我在我的應用程序中有一個NSTableView,數據被繪製在X軸和Y軸上(即,每行都與每列匹配)。我已經按照自己喜歡的方式填充了單元格的數據,但水平伸展的柱子看起來很糟糕。一種實現橫向NSTextFieldCell的方法?

我想將NSTextFieldCell放在一邊,以便文本垂直寫入而不是水平寫入。我意識到我可能不得不繼承NSTextFieldCell的子類,但我不確定哪些函數需要重寫才能完成我想要的操作。

NSTextFieldCell中的哪些函數可以繪製文本本身?有沒有任何內置的方式來垂直繪製文本而不是水平?

回答

0

好吧,我們花了很多時間來挖掘這個問題,但我最終遇到了NSAffineTransform對象,它顯然可以用來將整個座標系相對於應用程序進行移位。一旦我明白了這一點,我將NSTextViewCell分類並覆蓋-drawInteriorWithFrame:inView:函數以在繪製文本之前旋轉座標系。

- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView { 
    // Save the current graphics state so we can return to it later 
    NSGraphicsContext *context = [NSGraphicsContext currentContext]; 
    [context saveGraphicsState]; 

    // Create an object that will allow us to shift the origin to the center 
    NSSize originShift = NSMakeSize(cellFrame.origin.x + cellFrame.size.width/2.0, 
            cellFrame.origin.y + cellFrame.size.height/2.0); 

    // Rotate the coordinate system 
    NSAffineTransform* transform = [NSAffineTransform transform]; 
    [transform translateXBy: originShift.width yBy: originShift.height]; // Move origin to center of cell 
    [transform rotateByDegrees:270]; // Rotate 90 deg CCW 
    [transform translateXBy: -originShift.width yBy: -originShift.height]; // Move origin back 
    [transform concat]; // Set the changes to the current NSGraphicsContext 

    // Create a new frame that matches the cell's position & size in the new coordinate system 
    NSRect newFrame = NSMakeRect(cellFrame.origin.x-(cellFrame.size.height-cellFrame.size.width)/2, 
           cellFrame.origin.y+(cellFrame.size.height-cellFrame.size.width)/2, 
           cellFrame.size.height, cellFrame.size.width); 

    // Draw the text just like we normally would, but in the new coordinate system 
    [super drawInteriorWithFrame:newFrame inView:controlView]; 

    // Restore the original coordinate system so that other cells can draw properly 
    [context restoreGraphicsState]; 
} 

我現在有一個NSTextCell,它橫向地繪製它的內容!通過改變行高,我可以給它足夠的空間來看起來不錯。

相關問題