0

I am able to add padding for NSTextField using below technique, but same technique is not working for NSSecureTextField

class DesignableSecureTextFieldCell: NSSecureTextFieldCell {
    
    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.x = 10
       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)
   }
   
}
Willeke
  • 14,578
  • 4
  • 19
  • 47
Karan Rai
  • 11
  • 2
  • Have you tried [NSTextField Padding on the Left](https://stackoverflow.com/questions/38137824/nstextfield-padding-on-the-left)? – Willeke Feb 23 '22 at 12:51
  • Yes willeke I even tried, even this works on NSTextField, But not working on NSSecureTextField – Karan Rai Feb 24 '22 at 05:28
  • Thank you Willeke, Issue got solved https://stackoverflow.com/questions/9400686/vertically-centre-text-in-nssecuretextfield-with-subclassing from this – Karan Rai Feb 24 '22 at 10:22

0 Answers0