我想弄清楚如何在屏幕截圖上執行自定義NSTextField。任何人都可以至少指向一個嚮導或正確的方向嗎?我的意思是我明白你可以繪製背景和框,但是你如何告訴框它接受文本以及它應該是多大?如何使用文本輸入選項創建自定義NSTextField
任何幫助,高度讚賞。
我想弄清楚如何在屏幕截圖上執行自定義NSTextField。任何人都可以至少指向一個嚮導或正確的方向嗎?我的意思是我明白你可以繪製背景和框,但是你如何告訴框它接受文本以及它應該是多大?如何使用文本輸入選項創建自定義NSTextField
任何幫助,高度讚賞。
由於實際文字看起來沒有重疊邊框,因此一種做法是使用自定義視圖類來繪製邊框,並在其中放置NSTextView以進行文字輸入。只需將NSTextView設置爲無邊界並將背景顏色與視圖匹配即可。
如果只需要偶爾輸入你想要一個輕量級的控制,你甚至可以考慮使用一個標籤的文本或者乾脆讓一個NSString對象繪製自己與-drawInRext:withAttribures:
並抓住共同的NSTextField,並把它放在頂部編輯。這是NSTableView如何實現編輯。
我已經實現了類似的選項卡式控件,我支持通過雙擊(如excel選項卡)來編輯標題。這裏有一些代碼如何讓共享文本編輯器作爲靈感。然後,您需要通過委託方法來管理編輯過程。
-(BOOL)beginEditLabelInteractiveForTab:(NSInteger)index {
if (![self.window makeFirstResponder:self.window]) { // Try to make window first responder to ensure the shared field editor is available.
return NO;
};
NSTextView *tv = (NSTextView *)[self.window fieldEditor:YES forObject:nil]; // take the shared field editor and configure it
if (!tv) { // Did not get the field editor
return NO;
}
// Set font and location
[tv setVerticallyResizable:NO];
[tv setFont:self.textFont];
[tv setBackgroundColor:self.selectedFieldColor];
[tv setTextColor:self.editingColor];
[tv setEditable:YES];
editedTab = [self.tabs objectAtIndex:index];
tv.string = editedTab.label; // Set text
CGFloat w = editedTab.coreWidth + BSTeditorExtraPadding; // Slightly larger to fit the cursor
CGFloat h = (self.tabHeight < (self.preferredTextHeight-(2 * BSTstdYTextOffset))) ? (self.tabHeight-(2 * BSTstdYTextOffset)) : self.preferredTextHeight; // Text height or tab ht - 4 whichever is less
[tv setFrameSize:NSMakeSize(w,h)];
// x poistion is set in owner draw method to match location of tab
tv.textContainer.lineFragmentPadding = 0; // remove padding for alignment with text below
tv.delegate = self;
[self addSubview:tv];
[self.window makeFirstResponder:tv];
self.labelEditor = tv; // Store a reference also used as a flag
return YES;
}
老實說,我只看到做這件事的兩種方式:子類的NSTextField和皮膚到您的需求,但你將面臨很大的侷限性,或者創建自己的類,它看起來像一個的NSTextField。
在第二個選項中,繪製背景,邊框,標籤,圖標和光標。此外,您可以爲光標設置動畫,根據文本字段的狀態更改顏色和字體。這是很多工作要做,可能已經有一個很酷的圖書館在做你正在尋找的東西。
在這兩種情況下,您都需要管理鍵盤以使用所需的鍵盤(例如,我的意思是用於填寫電子郵件地址的電子郵件鍵盤)。