一.简介
按钮,主要用户通过NSControl
控制点击、高亮等事件,同iOS
的UIButton
@interface NSButton : NSControl <NSUserInterfaceValidations, NSAccessibilityButton, NSUserInterfaceCompression>
/**
* NSButton 定义于 AppKit 框架;
* NSButton 继承 NSControl
*/
二.源码
1.创建NSButton
// 创建带有标题和图像的标准按钮。在从左到右的本地化中,图像显示在标题的左侧。在从右到左的本地化中,它显示在右侧。
+ (instancetype)buttonWithTitle:(NSString *)title image:(NSImage *)image target:(nullable id)target action:(nullable SEL)action API_AVAILABLE(macos(10.12));
// 创建带有标题的标准按钮。
+ (instancetype)buttonWithTitle:(NSString *)title target:(nullable id)target action:(nullable SEL)action API_AVAILABLE(macos(10.12));
// 使用提供的图像创建标准按钮。设置图像的accessibilityDescription属性以确保此控件的可访问性。
+ (instancetype)buttonWithImage:(NSImage *)image target:(nullable id)target action:(nullable SEL)action API_AVAILABLE(macos(10.12));
// 使用提供的标题创建标准复选框。
+ (instancetype)checkboxWithTitle:(NSString *)title target:(nullable id)target action:(nullable SEL)action API_AVAILABLE(macos(10.12));
// 使用提供的标题创建标准单选按钮。s
+ (instancetype)radioButtonWithTitle:(NSsString *)title target:(nullable id)target action:(nullable SEL)action API_AVAILABLE(macos(10.12));
2.按钮基础配置
typedef NS_ENUM(NSUInteger, NSButtonType) {
NSButtonTypeMomentaryLight = 0,
NSButtonTypePushOnPushOff = 1,
NSButtonTypeToggle = 2,
NSButtonTypeSwitch = 3,// 勾选框,不支持带图片,适合做多选
NSButtonTypeRadio = 4,// 勾选框,不支持带图片,适合做单选。
NSButtonTypeMomentaryChange = 5,// 文字会闪烁
NSButtonTypeOnOff = 6,
NSButtonTypeMomentaryPushIn = 7,
NSButtonTypeAccelerator API_AVAILABLE(macos(10.10.3)) = 8,
NSButtonTypeMultiLevelAccelerator API_AVAILABLE(macos(10.10.3)) = 9,
};
// 设置按钮类型,如上NSButtonType枚举
- (void)setButtonType:(NSButtonType)type;
// 显示文本
@property (copy) NSString *title;
// 富文本
@property (copy) NSAttributedString *attributedTitle;
// 按钮打开状态时的标题,部分类型不显示备用标题
@property (copy) NSString *alternateTitle;
// 富文本按钮打开时的标题
@property (copy) NSAttributedString *attributedAlternateTitle;
// 按钮点击时的播放声音,默认为nil
@property (nullable, strong) NSSound *sound;
// 拖动时发送长按或长时间悬停时的操作。默认为否。
@property (getter=isSpringLoaded) BOOL springLoaded API_AVAILABLE(macos(10.10.3));
// 配置NSMultiLevelAcceleratorButton的最大允许级别,允许的值范围为[1,5]。默认为2。
@property NSInteger maxAcceleratorLevel API_AVAILABLE(macos(10.10.3));
// 为“continuous”为“YES”时发送的重复操作消息设置初始延迟和重复间隔(以秒为单位)。
- (void)setPeriodicDelay:(float)delay interval:(float)interval;
/*! 获取在“continuous”为YES时发送的重复操作消息的初始延迟和重复间隔(以秒为单位)。此方法的两个参数都不能为NULL。*/
- (void)getPeriodicDelay:(float *)delay interval:(float *)interval;
3.配置按钮图片
typedef NS_ENUM(NSUInteger, NSBezelStyle) {
NSBezelStyleRounded = 1,
NSBezelStyleRegularSquare = 2,
NSBezelStyleDisclosure = 5,
NSBezelStyleShadowlessSquare = 6,
NSBezelStyleCircular = 7,
NSBezelStyleTexturedSquare = 8,
NSBezelStyleHelpButton = 9,
NSBezelStyleSmallSquare = 10,
NSBezelStyleTexturedRounded = 11,
NSBezelStyleRoundRect = 12,
NSBezelStyleRecessed = 13,
NSBezelStyleRoundedDisclosure = 14,
NSBezelStyleInline API_AVAILABLE(macos(10.7)) = 15,
};
/*! 按钮系统边框样式 */
@property NSBezelStyle bezelStyle;
/*! 是否绘制边框,bordered为NO时,bezelStyle设置无效 */
@property (getter=isBordered) BOOL bordered;
/*! 按钮是否透明 */
@property (getter=isTransparent) BOOL transparent;
/*! 按钮是否仅在指针位于按钮上方时才显示其边框。 */
@property BOOL showsBorderOnlyWhileMouseInside;
/*! 按钮上的图片、设置nil时不显示图片 */
@property (nullable, strong) NSImage *image;
/*! 按钮处于打开状态上的替代图片,部分类型不支持 */
@property (nullable, strong) NSImage *alternateImage;
typedef NS_ENUM(NSUInteger, NSCellImagePosition) {
NSNoImage = 0,// 没有图片
NSImageOnly = 1,// 只显示图片
NSImageLeft = 2,// 图片在左
NSImageRight = 3,// 图片在右
NSImageBelow = 4,// 图片在下
NSImageAbove = 5,// 图片在上
NSImageOverlaps = 6,// 图片文字重叠
NSImageLeading API_AVAILABLE(macos(10.12)) = 7,// 前导
NSImageTrailing API_AVAILABLE(macos(10.12)) = 8 // 尾随
};
/*! 图片相对于标题的位置,NSCellImagePosition类型如是上枚举 */
@property NSCellImagePosition imagePosition;
typedef NS_ENUM(NSUInteger, NSImageScaling) {
NSImageScaleProportionallyDown = 0, // 如果图像对于目的地太大,则将其缩小。保持纵横比。
NSImageScaleAxesIndependently, // 缩放每个维度以完全适合目的地。不保留纵横比。
NSImageScaleNone, // 不缩放.
NSImageScaleProportionallyUpOrDown, // 将图像缩放到最大可能的尺寸,