Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set text vertical center in NSTextField

In a certain height NSTextField , the layout would be like this if text's size is small

enter image description here

the text is flowing to the top ,how to make the text center like this:

enter image description here

like image 260
Mil0R3 Avatar asked Aug 02 '12 09:08

Mil0R3


People also ask

How do I center text vertically in line height?

Use line-height for vertical centering with a fixed height To vertically center a single line of text or an icon within its container, we can use the line-height property. This property controls the height of a line in a run of text and adds an equal amount of space above and below the line-box of inline elements.

How do I center vertically in bootstrap?

1 — Vertical Center Using Auto Margins One way to vertically center is to use my-auto . This will center the element within it's flexbox container (The Bootstrap 4 . row is display:flex ). For example, h-100 makes the row full height, and my-auto will vertically center the col-sm-12 column.


1 Answers

class CustomTextFieldCell: NSTextFieldCell {      func adjustedFrame(toVerticallyCenterText rect: NSRect) -> NSRect {         // super would normally draw text at the top of the cell         var titleRect = super.titleRect(forBounds: rect)          let minimumHeight = self.cellSize(forBounds: rect).height         titleRect.origin.y += (titleRect.height - minimumHeight) / 2         titleRect.size.height = minimumHeight          return titleRect     }      override func edit(withFrame rect: NSRect, in controlView: NSView, editor textObj: NSText, delegate: Any?, event: NSEvent?) {         super.edit(withFrame: adjustedFrame(toVerticallyCenterText: rect), in: controlView, editor: textObj, delegate: delegate, event: event)     }      override func select(withFrame rect: NSRect, in controlView: NSView, editor textObj: NSText, delegate: Any?, start selStart: Int, length selLength: Int) {         super.select(withFrame: adjustedFrame(toVerticallyCenterText: rect), in: controlView, editor: textObj, delegate: delegate, start: selStart, length: selLength)     }      override func drawInterior(withFrame cellFrame: NSRect, in controlView: NSView) {         super.drawInterior(withFrame: adjustedFrame(toVerticallyCenterText: cellFrame), in: controlView)     }      override func draw(withFrame cellFrame: NSRect, in controlView: NSView) {         super.draw(withFrame: cellFrame, in: controlView)     } } 
like image 145
Sayanti Mondal Avatar answered Sep 23 '22 17:09

Sayanti Mondal