vertically aligning text in NSTableView row

前端 未结 4 1176
隐瞒了意图╮
隐瞒了意图╮ 2021-02-05 12:47

I have a small problem with NSTableView. When I am increasing height of a row in table, the text in it is aligned at top of row but I want to align it vertically centered!

4条回答
  •  忘掉有多难
    2021-02-05 13:08

    @iphaaw's answer updated for Swift 4 (note I also added "Cell" on the end of the class name for clarity, which also needs to match the class name 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)
        }
    }
    

提交回复
热议问题