swift - 圆角 NSTextField 用透明角绘制?

标签 swift macos cocoa interface-builder nstextfield

我的 NSTextField 有圆角和白色背景颜色,但 View 本身似乎是矩形的。这意味着圆角和边界矩形之间的空间显示为透明。我该如何填写该区域才能使其不突出?

图片:

The NSTextField with terrible corners.

代码:

let textFieldLayer = CALayer()
textField.layer = textFieldLayer
textField.backgroundColor = NSColor.whiteColor()
textField.layer?.backgroundColor = NSColor.whiteColor().CGColor
textField.layer?.borderColor = NSColor(calibratedRed: 47/255.0, green: 146/255.0, blue: 204/255.0, alpha: 1.0).CGColor
textField.layer?.borderWidth = 1
textField.layer?.cornerRadius = 7

最佳答案

我通过子类化 NSTextField 并重写它的 drawRect 方法来做到这一点:

override func drawRect(dirtyRect: NSRect) {
    super.drawRect(dirtyRect)
    var bounds: NSRect = self.bounds
    var border: NSBezierPath = NSBezierPath(roundedRect: NSInsetRect(bounds, 0.5, 0.5), xRadius: 3, yRadius: 3)
    NSColor(red: 47 / 255.0, green: 146 / 255.0, blue: 204 / 255.0, alpha: 1.0).set()
    border.stroke()
}

关于swift - 圆角 NSTextField 用透明角绘制?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38428265/

相关文章:

cocoa - 如何以编程方式从 AppDelegate 设置初始 ViewController?

objective-c - 为什么 NSRect 和 CGRect 是不兼容的类型?

ios - 每次我注销并再次登录时,我的表格 View 单元格都会重复

linux - 从 Mac iOS 到 Linux

ios - CFMutableArray 超出其容量

c++ - 使用 GLEW、GLFW 和 g++ 编译在 OS X 10.10 下启用 OpenGL 4.1

regex - 如何在 OSX 中 grep 三个句点?

ios - 从 TableView 中删除时索引超出范围?

ios - 为任何宽度类大小设置分段控制字体

ios - 使用 Realm 按多个属性排序