MacOS开发(OC)----NSButton类相关属性跟方法

一、NSButton的常用属性

   NSButton相关属性设置
   
   
    NSButton* button = [[NSButton alloc]init];
    button.frame = NSMakeRect(400, 200, 50, 20);
    
    //设置按钮名称
    button.title = @"HELLOW";
    
    //设置开启状态时按钮名称
    button.alternateTitle = @"abc";
    
    //设置按钮状态
    button.state  = NSControlStateValueOn;
    
    //设置按钮图片
    //button.image =
    
    //设置按钮开言启时图片
    //button.alternateImage =
    
    //设置图片和文字位置关系
    //button.imagePosition = NSImageLeft;
    
    //图片缩放设置
    //button.imageScaling = NSImageScaleNone;
    
    //设置按钮类型
    [button setButtonType:NSButtonTypeRadio];
    
    //设置是否显示边框
    //[button setBordered:false];
    
    //设置是否透明
    //[button setTransparent:YES];
    
    //设置高亮
    //[button highlight:YES];
    
    //设置快捷键
    //button.keyEquivalent =
    
    //设置快捷键掩码
    //button.keyEquivalentModifierMask =
    
    //设置按钮样式类
    //button.bezelStyle =

    //设置按键的tag值,这只是个标记值
    button.tag = 10;

二、在代码中设置NSButton事件响应

- (void)viewDidLoad {
    [super viewDidLoad];

    //设置按钮
    [self setbutton];
}


//增加按键
-(void)setbutton
{
    NSButton *button = [[NSButton alloc]init];

    //设置按键大小
    button.frame=CGRectMake(100, 100, 64, 64);

    //设置标题
    [button setTitle:@"确认签收"];

    //设置Target跟selector
    [button setTarget:self];
    [button setAction:@selector(buttonClick)];

    //增加到当前view中
    [self.view addSubview:button];
}


//事件响应函数
-(void)buttonClick
{
    NSLog(@"你点击了切换模式");
}

三、设置NSButton字体的颜色

      NSButton* button = [[NSButton alloc]init];
    button.frame = NSMakeRect(400, 200, 50, 20);
    
    //设置按钮名称
    button.title = @"HELLOW";


    //创始富文本
    NSMutableAttributedString *attrTitle = [[NSMutableAttributedString alloc]initWithAttributedString:[button attributedTitle]];


    //设置相关属性
    NSUInteger len = [attrTitle length];
    NSRange range = NSMakeRange(0, len);

    [attrTitle addAttribute:NSForegroundColorAttributeName
                      value:[NSColor redColor]
                      range:range];

    [attrTitle fixAttributesInRange:range];

    //改变按键的富文本
    [button setAttributedTitle:attrTitle];

四、改变按键背景颜色

//要先设置wnatLayer后才能设置颜色
self.wantsLayer = YES;
self.layer.backgroundColor = [NSColor blueColor].CGColor;

五、设置为不可点击


//直接设置enabled = NO。
button.enabled = NO;

六、设置按下和弹起时显示的图片或开启状态和关闭状态时的图片


    //比如你要设置按钮正常状态和点击后的图片不同,设置buttonType为Toggle,设置image和    
    //alternateImage,这样在正常状态就显示image的图片,在点击后就显示alternateImage的图片;
    [button setButtonType:NSButtonTypeToggle];
    button.image =
    button.alternateImage =


    比如你要设置按钮正常状态和点击时的图片不同,设置buttonType为change,设置image和
    //alternateImage,这样在正常状态就显示image的图片,在点击时就显示alternateImage的图片;

    [button setButtonType:NSButtonTypeMomentaryChange];
    button.image =
    button.alternateImage =

    //如果你要自定义类似系统关闭按钮那样的效果,就要自定义一个NSButton的子类,重写mouseDown,
    //mouseEnter,mouseExit等方法。

七、鼠标悬停在按钮上出现提示文字

button.toolTip = @"提示的文字";

 

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值