swiftxcodeswift3paddingnstextfield

NSTextField margin and padding? (Swift)


I am wondering if there’s a possibility to set a margin or padding for NSTextField?

I achieved a more or less custom looking textField (the first one in this screenshot)...

enter image description here

... using this code:

myTextField.wantsLayer = true
myTextField.layer?.cornerRadius = 2.0
myTextField.layer?.borderWidth = 1.0
myTextField.layer?.borderColor = CGColor(red: 0.69, green: 0.69, blue: 0.69, alpha: 1.0)

However, it feels like I have to add some padding on the left side so that the numbers are not too close to the border. Is that even possible?


Solution

  • I usually do this just by putting the textField in a container view. You can give the view any corner radius, border, etc you want, and then just constrain the textField to the inside with whatever padding you want. Just make sure you change the textField's border style to 'none' or you'll be able to see it inside your view.

    There might be a way to change the content inset like you can with a button, but I've never been able to find it.