Created
March 8, 2017 04:18
-
-
Save zpasternack/b8ae67901206bc6f80c9b0c65df8a8a8 to your computer and use it in GitHub Desktop.
Swift port of NSTextFieldCell subclass to vertically align text. Original code here: http://stackoverflow.com/questions/1235219/is-there-a-right-way-to-have-nstextfieldcell-draw-vertically-centered-text
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Makes the text in an NSTextFieldCell vertically centered. Works with single line, non-editable cells. | |
// Maybe doesn't work with others. | |
// Stolen from this stackoverflow question: | |
// http://stackoverflow.com/questions/1235219/is-there-a-right-way-to-have-nstextfieldcell-draw-vertically-centered-text | |
import Cocoa | |
class VerticallyCenteredTextFieldCell: NSTextFieldCell { | |
override func titleRect(forBounds rect: NSRect) -> NSRect { | |
var titleFrame = super.titleRect(forBounds: rect) | |
let titleSize = self.attributedStringValue.size() | |
titleFrame.origin.y = rect.origin.y + (rect.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) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment