【Swift Mac开发】继承NSButton并扩展功能(持续维护更新)
功能:
1、图片与文字的边距
2、移出移出更改背景颜色
3、快捷设置NSButton的字体颜色
4、快捷设置NSButton的背景颜色
// // BMButton.swift // BullMan // // Created by Apple on 2021/5/11. // import Foundation import Cocoa class BMButton : NSButton { var oldBackgroundColor : NSColor! //MARK:按钮图片的内边距 @IBInspectable var verticalImagePadding: CGFloat = 0 @IBInspectable var horizontalImagePadding: CGFloat = 0 override func draw(_ dirtyRect: NSRect) { let originalBounds = self.bounds defer { self.bounds = originalBounds } self.bounds = originalBounds.insetBy( dx: horizontalImagePadding, dy: verticalImagePadding ) initButtonUiAction() let trackingArea = NSTrackingArea( rect: self.bounds, options: [.mouseEnteredAndExited, .activeAlways], owner: self, userInfo: nil) self.addTrackingArea(trackingArea) super.draw(dirtyRect) } func initButtonUiAction() { self.isBordered = false self.bezelStyle = .texturedSquare } override var intrinsicContentSize: NSSize { var size = super.intrinsicContentSize size.width += self.horizontalImagePadding size.height += self.verticalImagePadding return size; } //MARK:设置鼠标移入的背景颜色 override func mouseEntered(with event: NSEvent) { let cell : NSButtonCell = self.cell! as! NSButtonCell cell.backgroundColor = NSColor.init(hexRGB: 0xf5f5f5) } //MARK:设置鼠标移出的被禁颜色 override func mouseExited(with event: NSEvent) { let cell : NSButtonCell = self.cell as! NSButtonCell cell.backgroundColor = oldBackgroundColor } } class BMButtonCell : NSButtonCell { @IBInspectable var imagePaddingLeft : CGFloat = 0 @IBInspectable var imagePaddingTop : CGFloat = 0 @IBInspectable var textPaddingLeft : CGFloat = 0 @IBInspectable var textPaddingTop : CGFloat = 0 override func drawImage(_ image: NSImage, withFrame frame: NSRect, in controlView: NSView) { let newFrame = NSRect.init( origin: .init(x: frame.minX + imagePaddingLeft, y: frame.minY + imagePaddingTop), size: frame.size) super.drawImage(image, withFrame: newFrame, in: controlView) } override func drawTitle(_ title: NSAttributedString, withFrame frame: NSRect, in controlView: NSView) -> NSRect { let newFrame = NSRect.init( origin: .init(x: frame.minX + textPaddingLeft, y: frame.minY + textPaddingTop), size: frame.size) super.drawTitle(title, withFrame: newFrame, in: controlView) return newFrame } } extension BMButton { //MARK:设置按钮的字体颜色 var titleTextColor : NSColor { get { let attrTitle = self.attributedTitle return attrTitle.attribute(NSAttributedString.Key.foregroundColor, at: 0, effectiveRange: nil) as! NSColor } set(newColor) { let attrTitle = NSMutableAttributedString(attributedString: self.attributedTitle) let titleRange = NSMakeRange(0, self.title.count) attrTitle.addAttributes([NSAttributedString.Key.foregroundColor: newColor], range: titleRange) self.attributedTitle = attrTitle } } //MARK:设置按钮的背景颜色 var backgroundColor : NSColor { get { return oldBackgroundColor } set(newColor) { oldBackgroundColor = newColor self.wantsLayer = true self.layer?.backgroundColor = newColor.cgColor } } }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 百万级群聊的设计实践
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 《HelloGitHub》第 107 期