网站地图    收藏   

主页 > 前端 > css教程 >

会变色的背景透明NSButton - html/css语言栏目:htm

来源:自学PHP网    时间:2015-04-14 14:50 作者: 阅读:

[导读] 效果图://ColorTextButton.h#import Cocoa/Cocoa.h@interface NSButton (ColorButton)- (void)setHoverColor:(NSColor *)textColor;- (void)setNormalColor:(NSColor *)textColor;- (void)setPushColo......

 效果图:

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

[html]
#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 

使用:

[html]
[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]]; 



摘自 CodeMachine的专栏

自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习

京ICP备14009008号-1@版权所有www.zixuephp.com

网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com

添加评论