效果图:

//ColorTextButton.h

#import <Cocoa/Cocoa.h>@interface NSButton (ColorButton)
- (void)setHoverColor:(NSColor *)textColor;
- (void)setNormalColor:(NSColor *)textColor;
- (void)setPushColor:(NSColor *)textColor;
- (void)setDisableColor:(NSColor *)textColor;
- (void)setHeightLight:(BOOL)b;
@end@interface CustomButton : NSButton
@end@interface ColorButtonCell : NSButtonCell
{BOOL bClick;
}
@property (nonatomic,retain) NSColor *normal;
@property (nonatomic,retain) NSColor *hover;
@property (nonatomic,retain) NSColor *push;
@property (nonatomic,retain) NSColor *disable;
@end

//ColorTextButton.m

#import "ColorTextButton.h"@interface NSButtonCell()
- (void)_updateMouseTracking;
@end@implementation ColorButtonCell
@synthesize normal,hover,push,disable;
- (NSColor *)textColor
{NSAttributedString *attrTitle = [self attributedTitle];NSUInteger len = [attrTitle length];NSRange range = NSMakeRange(0, MIN(len, 1)); // take color from first charNSDictionary *attrs = [attrTitle fontAttributesInRange:range];NSColor *textColor = [NSColor controlTextColor];if (attrs) {textColor = [attrs objectForKey:NSForegroundColorAttributeName];}return textColor;
}- (void)setTextColor:(NSColor *)textColor
{NSMutableAttributedString *attrTitle = [[NSMutableAttributedString alloc] initWithAttributedString:[self attributedTitle]];NSUInteger len = [attrTitle length];NSRange range = NSMakeRange(0, len);[attrTitle addAttribute:NSForegroundColorAttributeName value:textColor range:range];[attrTitle fixAttributesInRange:range];[self setAttributedTitle:attrTitle];[attrTitle release];
}- (void)awakeFromNib
{[self setBordered:NO];[self setButtonType:NSMomentaryChangeButton];[self setTitle:[self title]];[self setTextColor:[NSColor blackColor]];
}- (void)mouseEntered:(NSEvent *)event
{if (hover != nil) {[self setTextColor:hover];}
}- (void)mouseExited:(NSEvent *)event
{NSLog(@"mouseExited\n");if(!bClick){if (normal != nil) {[self setTextColor:normal];NSLog(@"[%@]mouseExited\n",self );}}
}- (void)mouseDown:(NSEvent *)theEvent
{if (push != nil) {[self setTextColor:push];bClick = YES;}
}- (void)mouseUp:(NSEvent *)theEvent
{if (hover != nil) {[self setTextColor:hover];}
}- (void)_updateMouseTracking
{[super _updateMouseTracking];if ([self controlView] != nil && [[self controlView] respondsToSelector:@selector(_setMouseTrackingForCell:)]) {[[self controlView] performSelector:@selector(_setMouseTrackingForCell:) withObject:self];}
}- (void)setHeightLight:(BOOL)b
{if(b){if (hover != nil) {[self setTextColor:hover];bClick = YES;}}else{if (normal != nil) {[self setTextColor:normal];bClick = NO; }}
}@end@implementation NSButton (ColorButton)- (void)setHoverColor:(NSColor *)textColor
{[[self cell] setHover:textColor];
}- (void)setNormalColor:(NSColor *)textColor
{[[self cell] setNormal:textColor];
}- (void)setPushColor:(NSColor *)textColor
{[[self cell] setPush:textColor];
}- (void)setDisableColor:(NSColor *)textColor
{[[self cell] setDisable:textColor];
}- (void)setHeightLight:(BOOL)b
{[[self cell] setHeightLight:b];[self setNeedsDisplay:YES];
}@end@implementation CustomButton- (void)mouseDown:(NSEvent *)theEvent
{[[self cell] mouseDown:theEvent];[super mouseDown:theEvent];[[self cell] mouseUp:theEvent];
}@end

使用:

    [musicButton setHoverColor:[NSColor colorWithDeviceRed:0.5059 green:0.7451 blue:0.1961 alpha:1]];[musicButton setPushColor:[NSColor colorWithDeviceRed:0.5059 green:0.7451 blue:0.1961 alpha:1]];[musicButton setNormalColor:[NSColor colorWithDeviceRed:0.2471 green:0.2471 blue:0.2471 alpha:1]];

会变色的背景透明NSButton相关推荐

  1. CSS 背景(background)(背景颜色color、背景图片image、背景平铺repeat、背景位置position、背景附着、背景简写、背景透明、链接导航栏综合案例)

    1. 背景颜色(color) background-color:颜色值; 默认的值是 transparent 透明的 示例代码: <!DOCTYPE html> <html lang ...

  2. MFC 加入背景图片并让控件背景透明

    /*加入背景图片*/ BOOL CTOOLDlg::OnEraseBkgnd(CDC* pDC) {  // TODO: 在此加入消息处理程序代码和/或调用默认值  CDialog::OnEraseB ...

  3. 让Flash背景透明兼容Firefox、IE 6和IE 7的代码

    添加代码: <param name="wmode" value="transparent" > 到 <object>-</obje ...

  4. Sublime如何设置背景透明

    Sublime如何设置背景透明 下载sublime 透明背景插件 我用的是git下载插件: git clone https://github.com/vhanla/SublimeTextTrans.g ...

  5. 关于png、背景透明疑难杂症综合帖

    前言 在web重构中,为了追求视觉效果,会经常使用标签背景透明.透明的png图片等,可惜ie6未死,所以经常会有这样那样的问题出现,下面我总结一下ie6下各种怪症和解决方法. 标签背景透明 常规方法 ...

  6. Android 自定义Dialog背景透明及显示位置设置

    先贴一下显示效果图,仅作参考: 代码如下: 1.自定义Dialog public class SelectDialog extends AlertDialog{public SelectDialog( ...

  7. WPF解决WindowsFormsHost背景透明

    项目案例:WPF使用WindowsFormsHost播放视频,视频上显示边框.字幕等特效: 难点问题 1.WindowsFormsHost不支持背景透明: 2.WPF Panel.ZIndex无效,W ...

  8. 关于Static控件背景透明时文本覆盖重影的问题

    2019独角兽企业重金招聘Python工程师标准>>> 关于Static控件背景透明时文本覆盖重影的问题 通过映射OnCtlColor消息,而后在该函数中使用pDC->SetB ...

  9. C# WinForm 自定义控件,DataGridView背景透明,TabControl背景透明

    注意: 以下代码,属性直接赋值的语法糖要vs2015以上才支持. 1 using System.ComponentModel; 2 using System.Drawing; 3 using Syst ...

最新文章

  1. Jvm 系列(十一)Java 语法糖背后的真相
  2. hdu4499 搜索
  3. Python之 sklearn:sklearn中的RobustScaler 函数的简介及使用方法之详细攻略
  4. CentOS 7 firewall防火墙命令
  5. Java正则表达式应用详解
  6. [20160704]Block recover using RMAN.txt
  7. 【3】测试用例设计-因果图
  8. Git教程——分支 (branch)
  9. 编译原理--运行时存储组织(自己看)
  10. python 之 del() 函数
  11. CImageList::GetImageInfo取到的bitmap不能用
  12. 计算机系女生电脑买什么牌子,适合女生的笔记本电脑 大学女生买什么电脑好...
  13. php 拼团_详解在微信公众平台里实现微信拼团功能的步骤
  14. 探究MYSQL之索引
  15. 计算机用户文件内存,win7用户文件夹太大占用大量内存空间怎么清除?
  16. stm32h750/stm32h743原理图和pcb源文件
  17. 小程序和Web项目的区别
  18. php laravel框架笔记
  19. Java JDK 1.8 下载及其版本说明 8u202(最后一个免费版)
  20. V 神呼吁宽大处理!以太坊开发者 Virgil Griffith 被判入狱 63 个月

热门文章

  1. 全面解说OCA全贴合优势
  2. MyBatisPlus 开启事务并交由 Springboot 管理
  3. 行为管理(锐捷睿易篇)
  4. 不用找,你想要的廊ps后期素材素材都在这里
  5. easyx的使用(1)
  6. linux命令(五)--打/解包、压缩/解压缩、安装软件
  7. check 和nocheck
  8. NS-3教程(2):NS-3简单介绍
  9. linux常用命令——ls
  10. postman批量运行和参数化