Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set border for NSTextField

Wow, I've really fallen down the rabbit hole. I'm trying to have text on the background of part of a UI and a text field as another part, e.g. the birthday in:

enter image description here

I then want to repurpose that text filed to allow text entry. So I do something like:

myTextFieldName.editable = true
myTextFieldName.backgroundColor = NSColor.textBackgroundColor()

and I get something like: enter image description here

Which is all well and good, but then I note the nice thin border around the text field below it. So I think, I need a border! I add one with:

myTextFieldName.bordered = true

...and I get:

enter image description here

Pow! What a hideous strange thick border! It doesn't look like the default text field border at all! For the life of me, I can't figure out how to make the border of my "activated" text field match that of the default. Any ideas?

Many thanks in advance!

like image 378
Dribbler Avatar asked Jan 20 '26 19:01

Dribbler


1 Answers

Need to set border and border color:

myTextFieldName.wantsLayer = true
myTextFieldName.layer?.borderColor = NSColor(red:204.0/255.0, green:204.0/255.0, blue:204.0/255.0, alpha:1.0).cgColor
myTextFieldName.layer?.borderWidth = 1.0
myTextFieldName.layer?.cornerRadius = 0.0

Set corner radius if you want rounded corner.

like image 132
Girish Chovatiya Avatar answered Jan 23 '26 13:01

Girish Chovatiya



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!