会变色的背景透明NSButton

效果图:


//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 char
    NSDictionary *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]];




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值