Set border for NSTextField

邮差的信 提交于 2019-12-11 01:54:53

问题


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:

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:

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:

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!


回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/38841251/set-border-for-nstextfield

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!