MAC OS X Cocoa NSToolbar 的使用
2005-03-04 15:29
295 查看
//向NSToolbar控件添加自定义的NSToolbarItem项
- (NSToolbarItem *) toolbar: (NSToolbar *)toolbar itemForItemIdentifier:
(NSString *) itemIdent willBeInsertedIntoToolbar:(BOOL) willBeInserted {
// Required delegate method Given an item identifier, self method
returns an item
// The toolbar will use self method to obtain toolbar items that can
be displayed in the customization sheet, or in the toolbar itself
NSToolbarItem *toolbarItem = [[[NSToolbarItem alloc]
initWithItemIdentifier: itemIdent] autorelease];
if ([itemIdent isEqual: SquareDotViewItemIdentifier]) {
SquareDotView *dv = [[SquareDotView alloc] initWithFrame:
NSMakeRect(0,0,100,32)];
[toolbarItem setView: dv];
// set a reasonable minimum size
[toolbarItem setMinSize: NSMakeSize(100,32)];
// set a maximum size that allows us to stretch.
[toolbarItem setMaxSize: NSMakeSize(300,32)];
[toolbarItem setLabel: @"Dot View"];
[toolbarItem setPaletteLabel: @"A Dot View"];
[toolbarItem setToolTip: @"This is a dot view"];
}
.....
}
//返回单击后能被选中且为高亮显示的NSToolbarItem的集合。
- (NSArray *)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar
{// return an array of all the items that can be highlight display after selected
return [NSArray arrayWithObjects:
NSColorToolbarItemIdentifier,
NSFontToolbarItemIdentifier,nil];
}
//返回程序加载后的默认的NSToolbarItem的集合。
- (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar *)toolbar
{ // return an array of the items found in the default toolbar
return [NSArray arrayWithObjects:
NSToolbarSeparatorItemIdentifier,
NSFontToolbarItemIdentifier,
NSToolbarCustomizeToolbarItemIdentifier,
nil];
}
//以下是作为NSToolbarItem出现的自定义类
The "SquareDotView" class:
@interface SquareDotView : NSView {
@private
float sdSide;
NSPoint sdLocation;
}
@end
@implementation SquareDotView
#define START_SIDE 10
#define START_LOCATION NSMakePoint(10,10)
- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
sdSide = START_SIDE;
sdLocation = START_LOCATION;
}
return self;
}
- (void)mouseDown:(NSEvent *)theEvent {
sdLocation = [self convertPoint: [theEvent locationInWindow]
fromView: nil];
[self setNeedsDisplay: YES];
}
- (void)drawRect:(NSRect)rect {
[[NSColor redColor] set];
[[NSBezierPath bezierPathWithRect: NSMakeRect(sdLocation.x,
sdLocation.y, sdSide, sdSide)] fill];
[[NSColor blackColor] set];
[[NSBezierPath bezierPathWithRect: [self bounds]] stroke];
}
- (id)initWithCoder:(NSCoder *)coder {
self = [super initWithCoder: coder];
if (self) {
[coder decodeValuesOfObjCTypes: "fff", &sdSide, &sdLocation.x,
&sdLocation.y];
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)coder {
[super encodeWithCoder: coder];
if (self) {
[coder encodeValuesOfObjCTypes: "fff", &sdSide, &sdLocation.x,
&sdLocation.y];
}
}
@end
- (NSToolbarItem *) toolbar: (NSToolbar *)toolbar itemForItemIdentifier:
(NSString *) itemIdent willBeInsertedIntoToolbar:(BOOL) willBeInserted {
// Required delegate method Given an item identifier, self method
returns an item
// The toolbar will use self method to obtain toolbar items that can
be displayed in the customization sheet, or in the toolbar itself
NSToolbarItem *toolbarItem = [[[NSToolbarItem alloc]
initWithItemIdentifier: itemIdent] autorelease];
if ([itemIdent isEqual: SquareDotViewItemIdentifier]) {
SquareDotView *dv = [[SquareDotView alloc] initWithFrame:
NSMakeRect(0,0,100,32)];
[toolbarItem setView: dv];
// set a reasonable minimum size
[toolbarItem setMinSize: NSMakeSize(100,32)];
// set a maximum size that allows us to stretch.
[toolbarItem setMaxSize: NSMakeSize(300,32)];
[toolbarItem setLabel: @"Dot View"];
[toolbarItem setPaletteLabel: @"A Dot View"];
[toolbarItem setToolTip: @"This is a dot view"];
}
.....
}
//返回单击后能被选中且为高亮显示的NSToolbarItem的集合。
- (NSArray *)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar
{// return an array of all the items that can be highlight display after selected
return [NSArray arrayWithObjects:
NSColorToolbarItemIdentifier,
NSFontToolbarItemIdentifier,nil];
}
//返回程序加载后的默认的NSToolbarItem的集合。
- (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar *)toolbar
{ // return an array of the items found in the default toolbar
return [NSArray arrayWithObjects:
NSToolbarSeparatorItemIdentifier,
NSFontToolbarItemIdentifier,
NSToolbarCustomizeToolbarItemIdentifier,
nil];
}
//以下是作为NSToolbarItem出现的自定义类
The "SquareDotView" class:
@interface SquareDotView : NSView {
@private
float sdSide;
NSPoint sdLocation;
}
@end
@implementation SquareDotView
#define START_SIDE 10
#define START_LOCATION NSMakePoint(10,10)
- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
sdSide = START_SIDE;
sdLocation = START_LOCATION;
}
return self;
}
- (void)mouseDown:(NSEvent *)theEvent {
sdLocation = [self convertPoint: [theEvent locationInWindow]
fromView: nil];
[self setNeedsDisplay: YES];
}
- (void)drawRect:(NSRect)rect {
[[NSColor redColor] set];
[[NSBezierPath bezierPathWithRect: NSMakeRect(sdLocation.x,
sdLocation.y, sdSide, sdSide)] fill];
[[NSColor blackColor] set];
[[NSBezierPath bezierPathWithRect: [self bounds]] stroke];
}
- (id)initWithCoder:(NSCoder *)coder {
self = [super initWithCoder: coder];
if (self) {
[coder decodeValuesOfObjCTypes: "fff", &sdSide, &sdLocation.x,
&sdLocation.y];
}
return self;
}
- (void)encodeWithCoder:(NSCoder *)coder {
[super encodeWithCoder: coder];
if (self) {
[coder encodeValuesOfObjCTypes: "fff", &sdSide, &sdLocation.x,
&sdLocation.y];
}
}
@end
相关文章推荐
- 最新Mac OS X 10.12.1 安装cocoapods及使用详解
- Mac OS X 10.11.1 安装cocoapods及使用详解
- 最新Mac OS X 10.12.1 安装cocoapods及使用详解
- Xcode7使用CocoaPods不工作, 在更新 Mac OS X EI Capitain之后
- Mac OS X安装cocoapods及使用详解
- 在更新 Mac OS X EI Capitain之后Xcode7使用CocoaPods不工作的解决方法
- [cocoa]Mac开发知识:NSButton使用及自定义/Safari调用/NSTextField
- 最新Mac OS X 10.11之后 安装cocoapods及使用详解
- 最新Mac OS X 10.12.1 安装cocoapods及使用详解
- 最新Mac OS X 10.12.1 安装cocoapods及使用详解
- 最新Mac OS X 10.12.1 安装cocoapods及使用详解
- 最新Mac OS X 10.12.1 安装cocoapods及使用详解
- 最新Mac OS X 10.12.1 安装cocoapods及使用详解
- 最新Mac OS X 10.12.1 安装cocoapods及使用详解
- iOS最新Mac OS X 10.11之后 安装cocoapods及使用详解
- Mac OS X 10.11.1 安装cocoapods及使用详解
- 在Mac OS X上使用HAXM加速Android Emulator
- 黑马程序员—虚拟机的安装、Mac OS X系统使用、五个OC小程序
- Mac OS X下使用Homebrew安装PNG支持——重新编译PHP
- Key-Value Coding, Key-Value observing(Chapter 7 of Cocoa Programming for Mac OS X)