前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >OS X开发:NSButton按钮控件应用 原

OS X开发:NSButton按钮控件应用 原

作者头像
珲少
发布2018-08-15 14:40:26
1.4K0
发布2018-08-15 14:40:26
举报
文章被收录于专栏:一“技”之长

OS X开发:NSButton按钮控件应用

    NSButton控件用来创建功能按钮,和UIButton相比,其样式要丰富许多。NSButton继承自NSControl,其使用setTarget与setAction来添加触发方法,如下:

代码语言:javascript
复制
    NSButton * btn = [[NSButton alloc]initWithFrame:CGRectMake(50, 300, 90, 25)];
    [btn setTarget:self];
    [btn setAction:@selector(click)];
    [self.view addSubview:btn];

NSButton类中常用属性和方法解析如下:

代码语言:javascript
复制
//设置按钮标题
@property (copy) NSString *title;
//设置按钮开启状态的标题
@property (copy) NSString *alternateTitle;
//设置按钮图片
@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(macosx(10.12)) = 7,   //正方向
    NSImageTrailing API_AVAILABLE(macosx(10.12)) = 8    //逆方向
};
*/
@property NSCellImagePosition imagePosition;
//设置图片缩放模式
/*
typedef NS_ENUM(NSUInteger, NSImageScaling) {
    NSImageScaleProportionallyDown = 0, // 下方缩放
    NSImageScaleAxesIndependently,      // 整体缩放
    NSImageScaleNone,                   // 不缩放.
    NSImageScaleProportionallyUpOrDown, // 上下缩放
    
    NSScaleProportionally NS_ENUM_DEPRECATED_MAC(10_0, 10_10, "Use NSImageScaleProportionallyDown instead") = 0,
    NSScaleToFit NS_ENUM_DEPRECATED_MAC(10_0, 10_10, "Use NSImageScaleAxesIndependently instead"),
    NSScaleNone NS_ENUM_DEPRECATED_MAC(10_0, 10_10, "Use NSImageScaleNone instead")
};
*/
@property NSImageScaling imageScaling NS_AVAILABLE_MAC(10_5);
//图片是否环绕标题
@property BOOL imageHugsTitle;
//设置按钮状态
/*
enum {
    NSMixedState = -1,  //混合状态
    NSOffState   =  0,  //关闭状态
    NSOnState    =  1,  //开启状态
};
*/
@property NSInteger state;
//设置是否显示边框
@property (getter=isBordered) BOOL bordered;
//设置按钮是否透明
@property (getter=isTransparent) BOOL transparent;
//设置快捷键
@property (copy) NSString *keyEquivalent;
//设置富文本标题
@property (copy) NSAttributedString *attributedTitle;
@property (copy) NSAttributedString *attributedAlternateTitle;
//设置边框风格
@property NSBezelStyle bezelStyle;
//设置是否当鼠标移动到按钮上时显示边框
@property BOOL showsBorderOnlyWhileMouseInside;
//设置按钮声音
@property (nullable, strong) NSSound *sound;

下面是一些便捷创建按钮的方法:

代码语言:javascript
复制
//创建标准的按钮 包括标题和图片
+ (instancetype)buttonWithTitle:(NSString *)title image:(NSImage *)image target:(nullable id)target action:(nullable SEL)action;
//创建文字按钮
+ (instancetype)buttonWithTitle:(NSString *)title target:(nullable id)target action:(nullable SEL)action;
//创建图片按钮
+ (instancetype)buttonWithImage:(NSImage *)image target:(nullable id)target action:(nullable SEL)action;
//创建复选框按钮
+ (instancetype)checkboxWithTitle:(NSString *)title target:(nullable id)target action:(nullable SEL)action;
//创建单选框按钮
+ (instancetype)radioButtonWithTitle:(NSString *)title target:(nullable id)target action:(nullable SEL)action;
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017/07/18 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • OS X开发:NSButton按钮控件应用
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档