NNButton 2.0.0

NNButton 2.0.0

shang1219178163 维护。




NNButton 2.0.0

  • shang1219178163

NNButton

License Platform Version

NSButton 封装,模拟 UIButton

NSButton 自定义,类似 iOS 的 UIButton 使用

代码

//
//  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
    NNButtonTypeOutlined = 1,      //backgroud: white , text: blue, has bordColor
    NNButtonTypeElevated = 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 目录中首先运行 pod install


lazy var btnFive: NNButton = {
    let view = NNButton(type: .typeText)
    view.setTitle("NNButton_typeText", for: .normal)
//        view.isEnabled = false
    view.viewBlock { (sender, state) in
        DDLog(sender, state)
    }
    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
}()
    
@objc func handleActionBtn(_ sender: NNButton) {
//        sender.selected = !sender.selected
//        DDLog("\(sender)_\(sender.selected)_\(sender.isHighlighted)")
    
//        sender.layer?.cornerRadius = sender.selected ? 10 : 0
}

需求

osx: 10.12

安装

NNButton 可通过 CocoaPods 安装。只需在 Podfile 中添加以下行即可

pod 'NNButton'

作者

shang1219178163,[email protected]

许可协议

NNButton 遵循 MIT 许可协议。有关更多信息,请参阅 LICENSE 文件。