Osheep

时光不回头,当下最重要。

《macOS开发》自定义控件之NSButton

控件功能简介:
1. 可设置(默认/选中)背景(图片/颜色)
2. 可设置(默认/选中)文字(内容/颜色/字体样式)
3. 当鼠标移动到控件位置时,鼠标变为"小手"
4. 当鼠标移动出控件,鼠标变为"箭头"
5. 基本的点击功能

#import <Cocoa/Cocoa.h>

@interface LLCustomBtn : NSView

@property (nullable, weak) id target;
@property (nullable) SEL action;

///按钮文字
@property (nullable, strong) NSString *defaultTitle;
@property (nullable, strong) NSString *selectedTitle;

///按钮文字颜色
@property (nullable, strong) NSColor  *defaultTitleColor;
@property (nullable, strong) NSColor  *selectedTitleColor;

///按钮字体
@property (nullable, strong) NSFont   *defaultFont;
@property (nullable, strong) NSFont   *selectedFont;

///当背景图片存在时,背景色无效
@property (nullable, strong) NSImage  *defaultBackgroundImage;
@property (nullable, strong) NSImage  *selectedBackgroundImage;

///当背景图片不存在时,显示背景色
@property (nullable, strong) NSColor  *defaultBackgroundColor;
@property (nullable, strong) NSColor  *selectedBackgroundColor;

@end

#import "LLCustomBtn.h"

@interface LLCustomBtn ()

@property (nonatomic,assign) BOOL mouseDown;

@end

@implementation LLCustomBtn

-(void)updateTrackingAreas{
    
    NSTrackingAreaOptions options = NSTrackingMouseEnteredAndExited|NSTrackingActiveInKeyWindow;
    NSTrackingArea *trackingArea = [[NSTrackingArea alloc] initWithRect:self.bounds
                                                                options:options
                                                                  owner:self
                                                               userInfo:nil];
    [self addTrackingArea:trackingArea];
}

-(void)mouseEntered:(NSEvent *)theEvent{
    [[NSCursor pointingHandCursor] set];
}

-(void)mouseExited:(NSEvent *)theEvent{
    [[NSCursor arrowCursor] set];
}

- (void)mouseDown:(NSEvent *)event {
    NSPoint point = [self convertPoint:[event locationInWindow] fromView:nil];
    if (CGRectContainsPoint(self.bounds, point)) {
        self.mouseDown = YES;
    }
}

- (void)mouseUp:(NSEvent *)event {
    if (self.mouseDown) {
        self.mouseDown = NO;
        [self setNeedsDisplay:YES];
        
        NSPoint point = [self convertPoint:[event locationInWindow] fromView:nil];
        if (CGRectContainsPoint(self.bounds, point)) {
            
            if (self.target &&
                self.action &&
                [self.target respondsToSelector:self.action]) {
                [self.target performSelector:self.action];
            }
        }
    }
}

- (void)setMouseDown:(BOOL)mouseDown {
    if (_mouseDown == mouseDown) return;
    
    _mouseDown = mouseDown;
    [self setNeedsDisplay:YES];
}

- (void)drawRect:(NSRect)dirtyRect {
    
    NSString *title      = nil;
    NSFont   *font       = nil;
    NSColor  *titleColor = nil;
    NSColor  *backgroundColor = nil;
    NSImage  *backgroundImage = nil;
    
    
    if (self.mouseDown) {
        title = self.selectedTitle;
        font  = self.selectedFont;
        titleColor = self.selectedTitleColor;
        backgroundColor = self.selectedBackgroundColor;
        backgroundImage = self.selectedBackgroundImage;
    }
    else {
        title = self.defaultTitle;
        font  = self.defaultFont;
        titleColor = self.defaultTitleColor;
        backgroundColor = self.defaultBackgroundColor;
        backgroundImage = self.defaultBackgroundImage;
    }
    
    if (backgroundImage) {
        [backgroundImage drawInRect:dirtyRect];
    }
    else if (backgroundColor) {
        [backgroundColor setFill];
        NSRectFill(dirtyRect);
    }
    
    if (title) {
        if (font == nil) {
            font = [NSFont systemFontOfSize:17];
        }
        
        if (titleColor == nil) {
            titleColor = [NSColor blackColor];
        }
        
        //绘制文字
        NSMutableAttributedString *attTitle = [[NSMutableAttributedString alloc] initWithString:title];
        
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
        paragraphStyle.lineSpacing = 1;
        paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
        NSDictionary *attributes = @{NSFontAttributeName:font,
                                     NSParagraphStyleAttributeName:paragraphStyle,
                                     NSForegroundColorAttributeName:titleColor};
        
        [attTitle addAttributes:attributes range:NSMakeRange(0, attTitle.length)];
        
        
        CGSize titleSize = 

.size; [attTitle drawInRect:CGRectMake((self.bounds.size.width-titleSize.width)/2.0, (self.bounds.size.height-titleSize.height)/2.0, titleSize.width, titleSize.height)]; } } @end

//使用方法
    LLCustomBtn *btn = [[LLCustomBtn alloc] initWithFrame:CGRectMake(400, 100, 100, 50)];
    btn.defaultTitle = @"未选中";
    btn.selectedTitle = @"已选中";
    btn.defaultTitleColor = [NSColor whiteColor];
    btn.selectedTitleColor = [NSColor blackColor];
    btn.defaultFont = [NSFont systemFontOfSize:10];
    btn.selectedFont = [NSFont systemFontOfSize:10];
    btn.defaultBackgroundColor = [NSColor greenColor];
    btn.selectedBackgroundColor = [NSColor blueColor];
    btn.defaultBackgroundImage = [NSImage imageNamed:@""];
    btn.selectedBackgroundImage = [NSImage imageNamed:@""];
    [btn setTarget:self];
    [btn setAction:@selector(btnCilc:)];
    [self.view addSubview:btn];

我们是伟大的程序员,我们天生爱分享!
觉得好,请给个star,谢谢!

点赞