美文网首页MacOS 开发收录
MacOS 开发(三):NSButton封装, 仿UIButto

MacOS 开发(三):NSButton封装, 仿UIButto

作者: SoaringHeart | 来源:发表于2020-03-23 18:51 被阅读0次
更新时间:2020-03-25 19:29:39

更新内容:增加多行标题支持


原生的NSButton用的很不习惯,关键是还很丑,通过一段时间的学习之后,NNButton实现了自己所需的效果,声明了最常用的三种样式(上边前三个按钮),调用简单,效果如下。

screenshots.jpeg
//
//  NNButton.h
//  MacTemplet
//
//  Created by Bin Shang on 2020/3/20.
//  Copyright © 2020 Bin Shang. All rights reserved.
//

#import <Cocoa/Cocoa.h>

typedef NS_OPTIONS(NSInteger, NNControlState) {
    NNControlStateNormal       = 1 << 0,
    NNControlStateHighlighted  = 1 << 1,
    NNControlStateDisabled     = 1 << 2,
    NNControlStateSelected     = 1 << 3,
    NNControlStateHover        = 1 << 4,
};

typedef NS_ENUM(NSInteger, NNButtonType) {
    NNButtonTypeText = 0,   //just text
    NNButtonType1 = 1,      //backgroud: white , text: blue, has bordColor
    NNButtonType2 = 2,      //backgroud: blue , text: white
};

NS_ASSUME_NONNULL_BEGIN


@interface NNButton : NSButton

+ (instancetype)buttonWithType:(NNButtonType)buttonType;

@property(nonatomic, assign) NNButtonType buttonType;
@property(nonatomic, copy) void(^block)(NNButton *sender, NNControlState state);

@property(nonatomic, assign) BOOL selected;
@property(nonatomic, assign) BOOL showHighlighted;
@property(nonatomic, assign) BOOL isAttributedTitle;

@property(nonatomic, strong) NSColor *titleColor;
@property(nonatomic, strong) NSColor *backgroundColor;
@property(nonatomic, strong) NSImage *backgroundImage;

- (void)setTitle:(nullable NSString *)title forState:(NNControlState)state;
- (void)setTitleColor:(nullable NSColor *)color forState:(NNControlState)state;

- (void)setAttributedTitle:(nullable NSAttributedString *)title forState:(NNControlState)state;
- (void)setBackgroundImage:(nullable NSImage *)image forState:(NNControlState)state;

- (void)setBorderColor:(nullable NSColor *)color forState:(NNControlState)state;
- (void)setBorderWidth:(nullable NSNumber *)number forState:(NNControlState)state;
- (void)setCornerRadius:(nullable NSNumber *)number forState:(NNControlState)state;

- (nullable NSString *)titleForState:(NNControlState)state;
- (nullable NSColor *)titleColorForState:(NNControlState)state;
- (nullable NSAttributedString *)attributedStringForState:(NNControlState)state;

- (nullable NSImage *)backgroundImageForState:(NNControlState)state;

- (nullable NSColor *)borderColorForState:(NNControlState)state;
- (nullable NSNumber *)borderWidthForState:(NNControlState)state;
- (nullable NSNumber *)cornerRadiusForState:(NNControlState)state;

///实时返回对应状态事件
- (void)stateBlock:(void(^)(NNButton *sender, NNControlState state))block;

@end

NS_ASSUME_NONNULL_END

Example

    lazy var btnFive: NNButton = {
        let view = NNButton(type: .typeText)
        view.setTitle("NNButton_typeText", for: .normal)
//        view.isEnabled = false

        view.addTarget(self, action: #selector(handleActionBtn(_:)))
        return view
    }()
    
    lazy var btnSix: NNButton = {
        let view = NNButton(type: .type1)
        view.setTitle("NNButton_type1", for: .normal)
        view.setTitleColor(NSColor.lightBlue, for: .normal)
//        view.isEnabled = false

        view.addTarget(self, action: #selector(handleActionBtn(_:)))
        return view
    }()
    
    lazy var btnSeven: NNButton = {
        let view = NNButton(type: .type2)
        view.setTitle("NNButton_type2", for: .normal)
//        view.isEnabled = false

        view.addTarget(self, action: #selector(handleActionBtn(_:)))
        return view
    }()
    
    lazy var btnEight: NNButton = {
        let view = NNButton(type: .type2)
        view.setTitle("NNButton_disabled", for: .normal)
        view.isEnabled = false

        view.addTarget(self, action: #selector(handleActionBtn(_:)))
        return view
    }()

    lazy var btnNine: NSButton = {
        let view = NSButton(title: "\n嗯,当不适合的文本自动以多行显示时,没有选择吗?我应该手动插入换行…\n", target: self, action: #selector(handleActionBtn(_:)))
        view.bezelStyle = .regularSquare
        view.lineBreakMode = .byCharWrapping

        return view
    }()

    lazy var btnTen: NNButton = {
        let view = NNButton(type: .type2)
        view.setTitle("嗯,当不适合的文本自动以多行显示时,没有选择吗?我应该手动插入换行…", for: .normal)
        view.font = NSFont.systemFont(ofSize: 13)

        view.addTarget(self, action: #selector(handleActionBtn(_:)))

        return view
    }()
    
    @objc func handleActionBtn(_ sender: NNButton) {
//        sender.selected = !sender.selected
//        DDLog("\(sender)_\(sender.selected)_\(sender.isHighlighted)")
        
//        sender.layer?.cornerRadius = sender.selected ? 10 : 0
    }

Github

相关文章

网友评论

    本文标题:MacOS 开发(三):NSButton封装, 仿UIButto

    本文链接:https://www.haomeiwen.com/subject/vsqgyhtx.html