Updated at: 2020-03-25 19:29:39

What’s New: Added support for multi-line headings


The native NSButton is very unaccustomed to use, the key is also very ugly, after a period of learning, NNButton achieved its desired effect, declared the three most commonly used styles (the top three buttons), simple to call, the effect is as follows.

// // 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_ENDCopy the code

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 Well, there is no choice when unsuitable text is automatically displayed on multiple lines? I should insert line breaks manually... \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(" Lazy var btnTen: NNButton = {let view = NNButton(type:.type2) view.settitle (" I should insert a newline manually..." , 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}Copy the code

Github