我是初學Cocoa程序員。我希望在程序中定位一個Cocoa NSTextField(自定義視圖的子視圖),旁邊是在自定義視圖上繪製的幾何圖形,該程序將在Lion上用XCode 4.3.2 for OS X開發。爲了讓我的問題簡單易懂,我們假設圖表是包含文本字段的框(除了NSTextField可用的邊框或邊框外,更遠,實際上,我的圖更加複雜)。我發現文本框和框不符合我的預期(請參閱下面的示例代碼)。爲什麼?我無法在NSBezierPath繪製的圖中正確定位NSTextField
我已經做了一個簡單的非基於文檔的項目作爲診斷示例,其中我已將自定義視圖拖動到應用程序窗口,添加了下面的代碼,並將IBOutlet連接到視圖:
AnAppDelegate.h:
#import <Cocoa/Cocoa.h>
@interface AnAppDelegate : NSObject <NSApplicationDelegate> {
IBOutlet NSView *view;
NSRect textFieldRect;
NSTextField *textField;
NSBezierPath *box;
}
@property (assign) IBOutlet NSWindow *window;
@end
AnAppDelegate.m:
#import "AnAppDelegate.h"
@implementation AnAppDelegate
@synthesize window = _window;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
textFieldRect = NSMakeRect(100, 100, 100, 20);
textField = [[NSTextField alloc] initWithFrame:textFieldRect];
NSRect frame = [textField frame];
NSLog(@"frame %f %f %f %f", frame.origin.x, frame.origin.y, frame.size.width, frame.size.height);
NSRect bounds = [textField bounds];
NSLog(@"bounds %f %f %f %f", bounds.origin.x, bounds.origin.y, bounds.size.width, bounds.size.height);
// Draw the text field
[textField setStringValue:@"My text field"];
[textField setBezeled:YES];
[textField setDrawsBackground:NO];
[textField setEditable:YES];
[textField setSelectable:YES];
[view addSubview:textField];
// Draw a box (rectangle) around the text field
//NSRect boxRect = [textField frame];
box = [[NSBezierPath alloc] init];
//boxRect.origin.x += 10;
//boxRect.origin.y += 10;
//boxRect.size.width += 20;
//boxRect.size.height += 20;
[box appendBezierPathWithRect:[textField frame]]; // :boxRect];
[box stroke];
}
@end
當該程序運行時,該框不移位到左邊和下面的文本字段,顯然是由每個維度中文本字段的高度。我期望它出現在NSTextField子視圖的「下面」,並被它隱藏起來。 (這是不理智的生產計劃,當然)。
現在,爲了更接近我想要的東西,如果取消註釋以上(附加boxRect到盒子路徑,而不是框架的源註釋掉線),那麼我的盒子就會從文本框架中出現10個單位 - 但我希望我不得不向原點增加-10個單位,而不是+10個單位。
這是怎麼回事?
康拉德,謝謝。儘管我花了一天的時間來試圖理解爲什麼有偏移,但我剛剛注意到,我的觀點被定位在(20,20)窗口中。如果我改變這一點,錯位會相應改變。但是我會按照你的建議來改正整體情況。 – user1336031 2012-04-16 12:02:22
這將在'-drawRect:'中無關,因爲座標系會匹配。 (您很少使用窗口的座標系。)修復整體問題,偏移量將消失。 – 2012-04-16 16:36:25