加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift – 具有圆角和背景颜色的NSButton

发布时间:2020-12-14 05:05:16 所属栏目:百科 来源:网络整理
导读:http://www.voidcn.com/article/p-srkqadwc-btx.html ? 我想要一个简单的按钮(带圆角的按钮),并添加背景.? 我已经尝试过两件事情: ? 1 – 使用圆形按钮图像:这是很好的,直到我需要缩放按钮,这导致圆形部分看起来丑陋. 2 – 扩展按钮并添加颜色 – 但是当我

http://www.voidcn.com/article/p-srkqadwc-btx.html

?

我想要一个简单的按钮(带圆角的按钮),并添加背景.?

我已经尝试过两件事情:

?

1 – 使用圆形按钮图像:这是很好的,直到我需要缩放按钮,这导致圆形部分看起来丑陋.

2 – 扩展按钮并添加颜色 – 但是当我点击按钮时,我有麻烦 – 我希望“推”状态与“常规”状态处于相同的颜色,但情况并非如此.

这是我用来扩展按钮的代码:

?

override func drawRect(dirtyRect: NSRect)

? ? {

? ? ? ? if let bgColor = bgColor {

? ? ? ? ? ? self.layer?.cornerRadius = 4

? ? ? ? ? ? self.layer?.masksToBounds = true

? ? ? ? ? ? self.layer?.backgroundColor = bgColor.CGColor

? ? ? ? ? ? bgColor.setFill()

?

? ? ? ? ? ? NSRectFill(dirtyRect)

? ? ? ? }

?

? ? ? ? super.drawRect(dirtyRect)

? ? }

无论如何,方法1和2都不工作,那么我该如何实现呢?

只是一个简单的按钮.. ??

编辑:

我在问OSX

?

覆盖NSButtonCell的drawWithFrame方法:

?

?

func drawWithFrame(cellFrame: NSRect,inView controlView: NSView) {

? ? var border = NSBezierPath(roundedRect: NSInsetRect(cellFrame,0.5,0.5),xRadius: 3,yRadius: 3)

? ? NSColor.greenColor().set()

? ? border.fill()

?

? ? var style = NSParagraphStyle.defaultParagraphStyle()

? ? style.alignment = NSCenterTextAlignment

? ? var attr = [NSParagraphStyleAttributeName : style,NSForegroundColorAttributeName : NSColor.whiteColor()]

?

? ? self.title.drawInRect(cellFrame,withAttributes: attr)

}

相关文章
  • 1.?android圆角矩形有背景颜色?
  • 2.?ios – NSAttributedString背景颜色和圆角?
  • 3.?css – 具有背景颜色的DIV上的圆角?
  • 4.?objective-c – 更改NSButton的背景颜色?
  • 5.?UIView背景颜色在Swift?
  • 6.?android – 具有边框颜色的圆角?
  • 7.?Android自定义TextView带圆角及背景颜色(动态改变圆角背景颜色)?
  • 8.?圆角背景?
  • 9.?会变色的背景透明NSButton?
  • 10.?android 圆角背景?
  • 更多相关文章...

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读