Mac一些常用控件以及简单在自定义View上绘制字符串
来源:互联网 发布:淘宝理财产品可靠吗 编辑:程序博客网 时间:2024/06/05 20:25
1. 首先创建一个MyView类,继承NSView,如下:
1
2
3
4
5
6
7
8
9
10
11
12
//
// MyView.h
// ManyControlTest
//
// Created by Himi on 12-6-6.
// Copyright (c) 2012年 Himi. All rights reserved.
//
#import <Cocoa/Cocoa.h>
@interface MyView : NSView
@end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
//
// MyView.m
// ManyControlTest
//
// Created by Himi on 12-6-6.
// Copyright (c) 2012年 Himi. All rights reserved.
//
#import "MyView.h"
@implementation MyView
- (id)initWithFrame:(NSRect)frame
{
self = [super initWithFrame:frame];
if
(self) {
// Initialization code here.
}
return
self;
}
- (
void
)drawRect:(NSRect)dirtyRect
{
NSString * str =@
"MyView --by Himi"
;
//属性包装设置
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
//设置字体样式
[dic setObject:[NSFont fontWithName:@
"Times"
size:14] forKey:NSFontAttributeName];
//设置字体颜色
[dic setObject:[NSColor redColor] forKey:NSForegroundColorAttributeName];
//绘制
[str drawAtPoint:NSMakePoint(50, 50) withAttributes:dic];
}
@end
代码很easy理解,不在赘述啦~
下面我们看一些基础常用控件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//
// AppDelegate.h
// ManyControlTest
//
// Created by Himi on 12-6-3.
// Copyright (c) 2012年 Himi. All rights reserved.
//
#import <Cocoa/Cocoa.h>
#import "MyView.h"
@interface AppDelegate : NSObject <NSApplicationDelegate,NSTabViewDelegate>
{
IBOutlet NSTextField *nfCount;
IBOutlet NSView *view ;
IBOutlet NSButton *btn;
IBOutlet NSPopUpButton *popBtn;
IBOutlet NSSegmentedControl * nsc;
IBOutlet NSForm *nForm;
IBOutlet NSMatrix * ms;
IBOutlet NSStepper * nsp;
IBOutlet NSTabView *tbView;
IBOutlet NSColorWell * nsWell;
IBOutlet MyView * myView;
}
-(IBAction)btnPress:(id)sender;
@property (assign) IBOutlet NSWindow *window;
@end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//
// AppDelegate.m
// ManyControlTest
//
// Created by Himi on 12-6-3.
// Copyright (c) 2012年 Himi. All rights reserved.
//
#import "AppDelegate.h"
@implementation AppDelegate
@synthesize window = _window;
- (
void
)applicationDidFinishLaunching:(NSNotification *)aNotification
{
//------绑定Delegate
[tbView setDelegate:self];
}
-(IBAction)btnPress:(id)sender{
//------ 处理NSButton的
if
(btn == sender){
[myView setHidden:YES];
}
//------处理NSPopUpButton
if
(popBtn == sender){
NSLog(@
"%@"
,[popBtn itemTitleAtIndex:0]);
NSLog(@
"%@"
,[popBtn itemTitleAtIndex:1]);
NSLog(@
"%@"
,[popBtn itemTitleAtIndex:2]);
}
//------处理 NSSegmentedControl
if
(nsc ==sender){
NSLog(@
"%i"
,[nsc isSelectedForSegment:0]);
NSLog(@
"%i"
,[nsc isSelectedForSegment:1]);
NSLog(@
"%i"
,[nsc isSelectedForSegment:2]);
}
//------处理 NSForm
if
(nForm == sender){
NSLog(@
"NSForm Cell 1 is %@"
,[[nForm cellAtIndex:0] stringValue]);
NSLog(@
"NSForm Cell 2 is %@"
,[[nForm cellAtIndex:1] stringValue]);
NSLog(@
"NSForm Cell 3 is %@"
,[[nForm cellAtIndex:2] stringValue]);
}
//------处理NSMatrix
if
(ms == sender){
NSLog(@
"NSMatrix is Select = %@"
,[[ms selectedCell] title]);
}
//-----处理 NSStepper
if
(nsp == sender){
NSString *string = [NSString stringWithFormat:@
"%i"
, (
int
)[nsp doubleValue]];
[nfCount setStringValue:string];
}
//-----处理 nsWell
if
(nsWell == sender){
NSColor* color = [nsWell color];
NSLog(@
"R=%f,G=%f,B=%f"
,[color greenComponent],[color redComponent],[color blueComponent]);
}
}
//------处理 tbView
//-(void)tabView:(NSTabView *)tabView didSelectTabViewItem:(NSTabViewItem *)tabViewItem{}
-(
void
)tabView:(NSTabView *)tabView willSelectTabViewItem:(NSTabViewItem *)tabViewItem{
if
([tbView indexOfTabViewItem:tabViewItem] == 0) {
NSLog(@
"view 111"
);
}
else
if
([tbView indexOfTabViewItem:tabViewItem] == 1) {
NSLog(@
"view 222"
);
}
}
@end
运行截图如下:
0 0
- Mac一些常用控件以及简单在自定义View上绘制字符串
- 【Cocoa(mac) Application 开发系列之二】总结一些常用控件以及简单在自定义View上绘制字符串
- 【Cocoa(mac) Application 开发系列之二】总结一些常用控件以及简单在自定义View上绘制字符串
- View绘制流程以及自定义控件
- 自定义View,在Canvas上绘制几何图形
- 自定义View将圆角矩形绘制在Canvas上
- android自定义View绘制字符串
- Android 自定义View以及View绘制详解
- 闲谈自定义控件源码-view如何展示在界面上
- View和自定义控件的绘制流程
- Android View绘制--自定义控件原理
- 自定义控件——绘制view
- 自定义控件View(三)___绘制饼状图
- Android自定义View drawText绘制截取字符串
- 一个简单自定义View控件
- 自定义View绘制简单基本图形
- 关于自定义组合控件以及自定义view
- 在屏幕上绘制字符串
- Python 字符串操作(截取/替换/查找/分割)
- STM32调试接口SWD连接
- android多屏幕适配
- IE10下Session丢失问题接军
- python在windows安装paramiko模块
- Mac一些常用控件以及简单在自定义View上绘制字符串
- linux环境变量
- java HashMap 循环遍历map的方法(转)
- IOS类继承关系图
- x86 x64
- Ubuntu下添加新用户
- 马云给北大学子讲创业:阿里为什么一直不提供班车和宿舍
- 关于Android中Adapter调用notifyDataSetChanged方法无效解析
- 微信云上已经入驻了17家服务商