macOS NSButton(上-属性)(UIbutton用于iOS)大全,欢迎大家提建议持续完善
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | - ( void )viewDidLoad { [ super viewDidLoad]; NSLog (@ "Hello world!" ); // 设置按钮 [ self setbutton]; } -( void )setbutton{ NSButton *button = [[ NSButton alloc]init]; button.frame=CGRectMake(100, 100, 64, 64); [button setTitle:@ "确认签收" ]; [button setTarget: self ]; [button setAction: @selector (buttonClick)]; [ self .view addSubview:button]; } -( void )buttonClick{ NSLog (@ "你点击了切换模式" ); } |
上面是小白模式,复制粘贴直接运行。
下面正式开发使用
#import "ViewController.h" @interface ViewController () /** macos 按钮 */ @property(nonatomic,weak) NSButton *btnnew; @end @implementation ViewController #pragma mark 懒加载 macos 按钮 - (NSButton *)btnnew { if (!_btnnew) { NSButton *btn = [[NSButton alloc] init]; [self.view addSubview:btn]; _btnnew = btn; } return _btnnew; } -(void)viewDidLoad { [super viewDidLoad]; CGFloat BtnY = 100; CGFloat BtnW = 100; CGFloat BtnH = 50; CGFloat BtnX = 200; self.btnnew.frame = CGRectMake(BtnX, BtnY, BtnW, BtnH); //////////////////////////////////////////////////////////////////////// // macOS 按钮的属性 // 1. 按钮文字这两种方式都可以 _btnnew.title = @"标题1"; [_btnnew setTitle:@"标题2"]; // 2.按钮阴影部分 _btnnew.layer.shadowOffset = CGSizeMake(1, 2); // 3.按钮带图标 _btnnew.image = [NSImage imageNamed:@"dream"]; // 4. 按钮触发的方法 //方法1 [_btnnew setTarget:self]; [_btnnew setAction:@selector(btnOnClick)]; //方法2 _btnnew.target = self; _btnnew.action = @selector(btnOnClick); // 5.开启状态文字 _btnnew.alternateTitle = @"选中"; // 6. 开启状态图片 _btnnew.alternateImage = [NSImage imageNamed:@"constellation"]; // 7.按钮的状态 _btnnew.state = 1; // 8.按钮图文位置 _btnnew.imagePosition = NSImageBelow; // 9.按钮设置图片缩放 _btnnew.imageScaling = NSImageScaleNone; // 10.按钮是否有边框 _btnnew.bordered = YES; // 11.按钮是否透明 _btnnew.transparent = NO; // 12以下设置的快捷键为:Shift + Command + P (如果设置的和系统的冲突,则不会触发,就好比微信的截图功能) // 快捷键 _btnnew.keyEquivalent = @"P"; // 快捷键掩码 [_btnnew setKeyEquivalentModifierMask:NSEventModifierFlagShift]; ///////////////////////////////////////////////////////////////////////////////////////// // 13. 按钮是否为高亮 _btnnew.highlighted = YES; // 14. 按钮的tag值,这只是个标记值 _btnnew.tag = 10; // 此处鸣谢客西电子提供参考 // 15.设置按钮字体的颜色
// (1)一般方法 // 创始富文本 NSMutableAttributedString *Mstr = [[NSMutableAttributedString alloc]initWithAttributedString:[_btnnew attributedTitle]]; // 设置相关属性 NSUInteger len = [Mstr length];//长度 NSRange range = NSMakeRange(0, len);//长度区间 [Mstr addAttribute:NSForegroundColorAttributeName value:[NSColor redColor] range:range]; [Mstr fixAttributesInRange:range]; // 改变按键的富文本 [_btnnew setAttributedTitle:Mstr]; // 替换文字,替换的位置和替换的内容 [Mstr replaceCharactersInRange:NSMakeRange(0, 1) withString:@"666"];
//(2)
// 1.无边框可以通过 contentTintColor 设置文字颜色
button.bordered = NO;
button.contentTintColor = NSColor.redColor;
// 2.有边框
NSMutableAttributedString *attrTitle = [[NSMutableAttributedString alloc] initWithAttributedString:[button attributedTitle]];
[attrTitle addAttribute:NSForegroundColorAttributeName value:NSColor.redColor range:NSMakeRange(0, attrTitle.length)];
[attrTitle fixAttributesInRange:NSMakeRange(0, attrTitle.length)];
[_btnnew setAttributedTitle:attrTitle];
//////////////////////////////////////////////////////////////////////////////////// // 16.改变按键背景颜色 // 要先设置wnatLayer后才能设置颜色 _btnnew.wantsLayer = YES; _btnnew.layer.backgroundColor = [NSColor blueColor].CGColor; // 17.设置为不可点击 _btnnew.enabled = NO; // 18. 设置按下和弹起时显示的图片或开启状态和关闭状态时的图片 //比如你要设置按钮正常状态和点击后的图片不同,设置buttonType为Toggle,设置image和 //alternateImage,这样在正常状态就显示image的图片,在点击后就显示alternateImage的图片; [_btnnew setButtonType:NSButtonTypeToggle]; _btnnew.image = [NSImage imageNamed:@"dream"]; _btnnew.alternateImage = [NSImage imageNamed:@"dream"]; // 比如你要设置按钮正常状态和点击时的图片不同,设置buttonType为change,设置image和 //alternateImage,这样在正常状态就显示image的图片,在点击时就显示alternateImage的图片; [_btnnew setButtonType:NSButtonTypeMomentaryChange]; _btnnew.image = [NSImage imageNamed:@"dream"]; _btnnew.alternateImage = [NSImage imageNamed:@"dream"]; //如果你要自定义类似系统关闭按钮那样的效果,就要自定义一个NSButton的子类,重写mouseDown, //mouseEnter,mouseExit等方法。 //////////////////////////////////////////////////////////////////////////////////// // 19. 鼠标悬停在按钮上出现提示文字 _btnnew.toolTip = @"提示的文字"; // 20.切圆角 此处鸣谢fancy啊fancy //方法1 _btnnew.layer.masksToBounds = YES; _btnnew.layer.cornerRadius = 30; //这里的30是你想设置的圆角大小,比如是一个40*40的正方形,那个设置成20就是一个圆,以此类推 //方法2 [_btnnew.layer setMasksToBounds:YES]; [_btnnew.layer setCornerRadius:16]; //这里的16是你想设置的圆角大小,比如是一个40*40的正方形,那个设置成20就是一个圆,以此类推 } #pragma mark 按钮触发事件 - (void)btnOnClick{ // 处理点击事件 NSLog(@"点击了我"); }
大家按自己需求使用
分类:
oc学习笔记
, mac os开发笔记
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix