在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
NSPopUpButton 下拉列表按钮,有两种。一种只有下拉箭头的按钮,另一种是既有上拉,也有下拉。
## ****下面以只有下拉箭头的按钮为例进行说明: // pullsDown 设置为 YES 只有向下的箭头 NSPopUpButton *popBtn = [[NSPopUpButton alloc] initWithFrame:CGRectMake(0, 100, 100, 30) pullsDown:YES]; [popBtn addItemWithTitle:@"城市"]; [popBtn addItemWithTitle:@"上海"]; [popBtn addItemWithTitle:@"广州"]; [popBtn addItemWithTitle:@"深圳"]; [popBtn addItemWithTitle:@"河南"];
[self.view addSubview:popBtn];
// popBtn 的点击事件 [popBtn setTarget:self]; [popBtn setAction:@selector(handlePopBtn:)];
NSPopUpButton 选中item的相应事件: - (void)handlePopBtn:(NSPopUpButton *)popBtn { // 选中item 的索引 NSLog(@"%d", popBtn.indexOfSelectedItem); // [popBtn selectItemAtIndex:popBtn.indexOfSelectedItem]; popBtn.title = popBtn.selectedItem.title; }
小礼物走一走,来简书关注我
NSPopUpButton创建菜单与子菜单 NSPopUpButton *popup = [[NSPopUpButton alloc] initWithFrame:
[Cocoa]_[初级]_[NSPopUpButton重绘实例] 2015年04月28日 19:37:57 Foreveroriginal 阅读数:712
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/moqj_123/article/details/45341071 场景:比如说,定义一个支持多种格式的控件,包含下拉菜单进行多种选择.由于标准的控件在界面上布局和自己所需的相差很多,所以要对控件进行重绘。 一下是具体实例: 类似于带下拉菜单的文本编辑框。 // MqjPopUpButton.m // MqjEditContact // // Created by mac-d on 4/28/15. // Copyright (c) 2015 mac-d. All rights reserved. // #import "MqjPopUpButton.h" @implementation MqjPopUpButton - (id)initWithFrame:(NSRect)frame { self = [super initWithFrame:frame]; if (self) { // Initialization code here. } return self; } -(BOOL) isFlipped { return YES; } - (void)drawRect:(NSRect)dirtyRect { // Drawing code here. //[super drawRect:dirtyRect]; [[NSColor colorWithCalibratedRed: 255/255.0 green:255/255.0 blue:255/255.0 alpha:1] setFill]; NSRectFill(dirtyRect); NSString *path =[[NSBundle mainBundle] pathForResource:@"down" ofType:@"png"]; NSImage *image =[[NSImage alloc] initWithContentsOfFile:path]; NSRect rect =NSZeroRect; rect.size = [image size]; NSPoint p = dirtyRect.origin; p.x +=dirtyRect.size.width-image.size.width-4; p.y +=(dirtyRect.size.height-image.size.height)/2; [image drawInRect:NSMakeRect(p.x, p.y, image.size.width, image.size.height) fromRect:rect operation:NSCompositeSourceOver fraction:1.0 respectFlipped:YES hints:nil]; //画横线 NSBezierPath *line = [NSBezierPath bezierPath]; [line setLineWidth:1]; [[NSColor colorWithCalibratedRed: 147/255.0 green:147/255.0 blue:147/255.0 alpha:0.5] setStroke]; NSPoint endPoint = dirtyRect.origin; endPoint.x += dirtyRect.size.width; [line moveToPoint:dirtyRect.origin]; [line lineToPoint:endPoint]; [line stroke]; [line moveToPoint:NSMakePoint(dirtyRect.origin.x, dirtyRect.origin.y+dirtyRect.size.height)]; [line lineToPoint:NSMakePoint(endPoint.x,dirtyRect.origin.y+dirtyRect.size.height)]; [line stroke]; //画竖线 [line moveToPoint:dirtyRect.origin]; [line lineToPoint:NSMakePoint(dirtyRect.origin.x,dirtyRect.origin.y+dirtyRect.size.height)]; [line stroke]; [line moveToPoint:endPoint]; [line lineToPoint:NSMakePoint(endPoint.x,dirtyRect.origin.y+dirtyRect.size.height)]; [line stroke]; //对选中的item设置勾选 NSArray *array = [super itemArray]; for(NSMenuItem *item in array) { if (item == [self selectedItem]) { [item setState:NSOnState]; } else { [item setState:NSOffState]; } } NSString *title =[[super selectedItem] title]; if (title == nil) { title = @""; } NSLog(@"title:%@",title);
NSSize titleSize = [title sizeWithAttributes:[NSDictionary dictionaryWithObject:[self font] forKey:NSFontAttributeName]]; CGFloat titleY = dirtyRect.origin.y + (dirtyRect.size.height - titleSize.height)/2; NSRect rectTitle = dirtyRect; rectTitle.origin = NSMakePoint(10, titleY); rectTitle.size.height = titleSize.height; [title drawInRect:rectTitle withAttributes:nil]; }
在MainMenu.xib布局一个NSPopUpButton,class:改为如图所示。 运行结果:
|
请发表评论