2013-11-22 121 views
1

我有一個的NSTextField在容器:插入點缺失

[textField setFrameOrigin:NSMakePoint(0, -t.frame.size.height)]; 
content = [[NSView alloc] init]; 
[content setWantLayer:YES] 
content.layer=[CALayer layer]; 
[content addSubview:textField]; 
[content scaleUnitSquareToSize:NSMakeSize(1, -1)]; 
content.frame=textField.frame; 
content.layer.backgroundColor=textBGColor.CGColor; 

本身坐落在一個視圖與

[view scaleUnitSquareToSize:NSMakeSize(1, -1)]; 

這一切都是爲了獲得左上原點的容器TextField和它的工作很好,唯一的問題在於InsertionPoint沒有繪製(至少不在可見框架中)。

我認爲InsertionPoint不是縮放或用TextField轉換。其他可能性是InsertionPoint不能在支持層的View中繪製。

有沒有辦法顯示InsertionPoint光標?

編輯

嘗試了一切準備後,似乎被定位出superviews範圍及其dirtyDrawRect的,因爲它的框架InsertionPoint(和focusRing)不會畫畫。有沒有辦法刪除剪輯的NSView?我需要能夠將我的TextField放置在每個絕對位置上。

回答

1

我找到了一個方法:自己實現繪圖。

1)給出一個自定義TextView作爲窗口的編輯器。

- (id)windowWillReturnFieldEditor:(NSWindow *)sender toObject:(id)anObject 
{ 

    if (!myCustomFieldEditor) { 
     myCustomFieldEditor = [[TextView alloc] init]; 
     [myCustomFieldEditor setFieldEditor:YES]; 
    } 
    return myCustomFieldEditor; 
} 

2)在自定義TextView類中繪製drawInsertionPoint方法。

-(void)drawInsertionPointInRect:(NSRect)rect color:(NSColor *)color turnedOn:(BOOL)flag{ 
    [color set]; 
    NSRectFill(rect); 
    [super drawInsertionPointInRect:rect color:color turnedOn:flag]; 
} 
0

對於插入點,只需將您的文本字段設置爲第一響應者即可。

[myTextField becomeFirstResponder]; 
+1

當我單擊TextField內部時,TextField應自動變爲FirstResponder。我可以編輯/選擇(選擇正在繪製)/複製文本,只是InsertionPoint不繪製。 – junghans