Dribbler
Dribbler

Reputation: 4681

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!

Upvotes: 3

Views: 4060

Answers (2)

Girish Chovatiya
Girish Chovatiya

Reputation: 195

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.

Upvotes: 3

Udaya Sri
Udaya Sri

Reputation: 2452

You can add borders to NSTextfield and customise it according how you want it.

 let border = CALayer()
 border.borderColor =  NSColor.gray.cgColor
 border.autoresizingMask = [.layerHeightSizable, .layerWidthSizable]
 border.borderWidth = 1.0

 myTextFieldName.wantsLayer = true
 myTextFieldName.layer?.addSublayer(border)
 myTextFieldName.layer?.masksToBounds = true

Upvotes: 1

Related Questions