使用NSTextView伪造NSTextField以获得漂亮的颜色?

尝试更改NSTextField的选定文本背景颜色(我们有一个黑暗的UI,选定的文本背景几乎与文本本身相同),但只有NSTextView似乎允许我们更改它。 因此我们尝试使用NSTextView伪造NSTextField,但无法使文本滚动工作相同。 我们得到的最接近的是这段代码:
NSTextView *tf = [ [ NSTextView alloc ] initWithFrame: NSMakeRect( 30.0, 20.0, 80.0, 22.0 ) ];

// Dark UI
[tf setTextColor:[NSColor whiteColor]];
[tf setBackgroundColor:[NSColor darkGrayColor]];

// Fixed size
[tf setVerticallyResizable:FALSE];
[tf setHorizontallyResizable:FALSE];

[tf setAlignment:NSRightTextAlignment]; // Make it right-aligned (yup, we need this too)

[[tf textContainer] setContainerSize:NSMakeSize(2000, 20)]; // Try to Avoid line wrapping with this ugly hack
[tf setFieldEditor:TRUE]; // Make Return key accept the textfield

// Set text properties
NSMutableDictionary *dict = [[[tf selectedTextAttributes] mutableCopy ] autorelease];
[dict setObject:[NSColor orangeColor] forKey:NSBackgroundColorAttributeName];
[tf setSelectedTextAttributes:dict];
这几乎没有问题,除非文本比文本字段长,否则无法以任何方式滚动到文本字段。 任何想法如何实现这一目标? 提前致谢 编辑:Joshua Nozzi在下面提出的解决方案 感谢Joshua,这是我正在寻找的一个很好的解决方案:
@interface ColoredTextField : NSTextField
- (BOOL)becomeFirstResponder;
@end

@implementation ColoredTextField
- (BOOL)becomeFirstResponder
{
    if (![super becomeFirstResponder])
        return NO;

    NSDictionary * attributes = [NSDictionary dictionaryWithObjectsAndKeys : 
                     [NSColor orangeColor], NSBackgroundColorAttributeName, nil];

    NSTextView * fieldEditor = (NSTextView *)[[self window] fieldEditor:YES forObject:self];
    [fieldEditor setSelectedTextAttributes:attributes];
    return YES;
}
@end
它不是用NSTextView伪装,而只是一个NSTextField,当它成为第一响应者时改变所选的文本颜色。 编辑:一旦您在文本字段中按Enter键,上面的代码将回退到默认选择颜色。这是一种避免这种情况的方法。
@interface ColoredTextField : NSTextField
- (BOOL)becomeFirstResponder;
- (void)textDidEndEditing:(NSNotification *)notification;

- (void)setSelectedColor;
@end

@implementation ColoredTextField
- (BOOL)becomeFirstResponder
{
    if (![super becomeFirstResponder])
        return NO;
    [self setSelectedColor];
    return YES;
}

- (void)textDidEndEditing:(NSNotification *)notification
{
    [super textDidEndEditing:notification];
    [self setSelectedColor];
}

- (void) setSelectedColor
{
    NSDictionary * attributes = [NSDictionary dictionaryWithObjectsAndKeys : 
                                [NSColor orangeColor], NSBackgroundColorAttributeName, nil];

    NSTextView * fieldEditor = (NSTextView *)[[self window] fieldEditor:YES forObject:self];
    [fieldEditor setSelectedTextAttributes:attributes];
}
@end
    
已邀请:
为什么不直接在字段成为第一响应者时设置文本字段的字段编辑器的属性? 在典型的文本字段中,选择仅在字段为第一响应者时可见,因此如果您要求文本字段的字段编辑器,然后设置其选定的文本属性,您将获得相同的效果,不是吗?
NSDictionary * attributes = [NSDictionary dictionaryWithObjectsAndKeys:
    [NSColor orangeColor], NSBackgroundColorAttributeName, nil];
NSTextView * fieldEditor = (NSTextView *)[[self window] fieldEditor:YES 
    forObject:textField];
[fieldEditor setSelectedTextAttributes:attributes];
注意:正如下面的评论中所讨论的,文档说明字段编辑器是NSTextView的实例是正确的,但是[NSWindow fieldEditor:forObject:]方法声称返回NSText(NSTextView的直接超类)。如果您计划发送字段编辑器NSTextView-only方法,则需要将其强制转换为NSTextView以安静编译器的警告。如果将来修改方法原型,那么转换不会破坏代码中的任何内容,因此可以安全地保留它。     

要回复问题请先登录注册