gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 13:13:58 36 4
gpt4 key购买 nike

我的 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/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com