zoukankan      html  css  js  c++  java
  • oc之mac开发

    NSButtonTypeSwitch 就是勾选样式
    其他样式可参考:http://blog.csdn.net/lovechris00/article/details/77976480
    allowsMixedState 代表是否可以混合选择。YES-有三种状态,-1、1、0;NO-2种状态,1、0。
    使用 setAction 来监听。
    只设置 buttonType 即可,不用设置 bezielType,设置了也无效。存疑: 这两者如何组合使用?
    - (void)addCheckBtn{

    NSButton *btn0 = [[NSButton alloc]init];

    btn0.frame = NSMakeRect(100, 100, 100, 100);

    btn0.wantsLayer = YES;
    btn0.layer.backgroundColor = [NSColor cyanColor].CGColor;

    [btn0 setButtonType:NSButtonTypeSwitch];

    //YES-有三种状态,-1、1、0
    //NO-2种状态,1、0
    btn0.allowsMixedState = YES;

    [self.window.contentView addSubview:btn0];

    [btn0 setAction:@selector(valueChange:)];

    }

    - (void)valueChange:(NSButton *)sender{

    NSButton *checkBtn = sender;
    BOOL isOn = checkBtn.state;
    NSLog(@" %d",isOn);
    }
    ---------------------
    作者:lovechris00
    来源:CSDN
    原文:https://blog.csdn.net/lovechris00/article/details/77977824
    版权声明:本文为博主原创文章,转载请附上博文链接!

    NSButtonType

    typedef NS_ENUM(NSUInteger, NSButtonType) {
    NSButtonTypeMomentaryLight = 0,
    NSButtonTypePushOnPushOff = 1,
    NSButtonTypeToggle = 2,
    NSButtonTypeSwitch = 3,
    NSButtonTypeRadio = 4,
    NSButtonTypeMomentaryChange = 5,
    NSButtonTypeOnOff = 6,
    NSButtonTypeMomentaryPushIn = 7,
    NSButtonTypeAccelerator NS_ENUM_AVAILABLE_MAC(10_10_3) = 8,
    NSButtonTypeMultiLevelAccelerator NS_ENUM_AVAILABLE_MAC(10_10_3) = 9,
    };

    显示结果

    小结:
    - 1、6 类型(NSButtonTypePushOnPushOff、NSButtonTypeOnOff),选中状态是蓝色,再次点击才会恢复到原来的颜色。
    - 0、2、7(NSButtonTypeMomentaryLight、NSButtonTypeToggle、NSButtonTypeMomentaryPushIn) 点击时会有背景色(高亮状态)。
    - 5(NSButtonTypeMomentaryChange) 点击时会有高亮状态,文字一闪,但是没有高亮的背景色。
    - 3(NSButtonTypeSwitch) 适合做多选
    - 4 (NSButtonTypeRadio) 适合做单选。

     按钮-无文字2.png

    测试代码

    - (void)addSerialBtn2{

    CGFloat btnW = 80;
    CGFloat btnH = 40;

    for (int i = 0; i < 8; i++) {
    NSButton *btn = [[NSButton alloc]initWithFrame:NSMakeRect( 20 + (i % 5) * (btnW + 5) ,50 + (i / 5) * (btnH + 40), btnW, btnH)];

    // btn.bezelStyle = i;

    btn.bezelStyle = NSRoundedBezelStyle;

    [btn setButtonType:i];

    NSString *btnName = [NSString stringWithFormat:@"按钮 - %d",i];
    [btn setTitle:@"按钮"];
    // [btn setTitle:@""];
    btn.wantsLayer = YES;
    btn.layer.backgroundColor = [NSColor cyanColor].CGColor;
    [self.window.contentView addSubview:btn];

    NSTextField *field = [[NSTextField alloc]initWithFrame:NSMakeRect(CGRectGetMinX(btn.frame), CGRectGetMinY(btn.frame) - 22, btnW, 20)];
    field.stringValue = btnName;
    field.bezelStyle = i;
    [self.window.contentView addSubview:field];

    }
    }
    ---------------------

  • 相关阅读:
    Git问题(Your local changes to the followingcant checkout because of unmerged files)
    Java实体映射工具 MapStruct
    获取一段代码执行花费的时长
    mybatis的xml中trim标签
    SQL语句统计每天、每月、每年的数据
    Java常见异常
    跨域与跨域访问
    java8新特性
    java中i=i++问题分析
    钉钉——第三方应用嵌入钉钉【微应用】
  • 原文地址:https://www.cnblogs.com/sundaymac/p/10334170.html
Copyright © 2011-2022 走看看