2016-01-18 54 views

回答

0

由于实际文字看起来没有重叠边框,因此一种做法是使用自定义视图类来绘制边框,并在其中放置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; 

}

0

老实说,我只看到做这件事的两种方式:子类的NSTextField和皮肤到您的需求,但你将面临很大的局限性,或者创建自己的类,它看起来像一个的NSTextField。

在第二个选项中,绘制背景,边框,标签,图标和光标。此外,您可以为光标设置动画,根据文本字段的状态更改颜色和字体。这是很多工作要做,可能已经有一个很酷的图书馆在做你正在寻找的东西。

在这两种情况下,您都需要管理键盘以使用所需的键盘(例如,我的意思是用于填写电子邮件地址的电子邮件键盘)。

相关问题