用NSTextView伪造NSTextField以获得漂亮的颜色? [英] Faking an NSTextField using an NSTextView to get nice coloring?

查看:87
本文介绍了用NSTextView伪造NSTextField以获得漂亮的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试更改NSTextField的选定文本背景颜色(我们有一个深色的UI,并且选定的文本背景与文本本身几乎相同),但是只有NSTextView似乎允许我们对此进行更改.

Trying to change the selected text background color for an NSTextField (we have a dark UI, and selected text background is almost the same as the text itself), but only NSTextView seems to allow us to change this.

因此,我们试图使用NSTextView伪造NSTextField,但无法使文本滚动正常工作.

So we're trying to fake an NSTextField using an NSTextView, but can't get text scrolling to work the same.

我们得到的最接近的是以下代码:

The closest we get is with this code:

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];

这几乎可以正常工作,除了文本长于文本字段之外,您无法以任何方式滚动到它.

This works almost alright, except that if the text is longer than the text field, you can't scroll to it in any way.

关于如何实现此目标的任何想法?

Any idea as how to accomplish this?

预先感谢

下面的 Joshua Nozzi

感谢约书亚,这是我所寻找的一个很好的解决方案:

Thanks to Joshua, this is a great solution to what I was looking for:

@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,它在成为第一响应者时会更改所选文本的颜色.

Instead of faking it with an NSTextView, it's just an NSTextField that changes the selected text color when it becomes first responder.

编辑:在文本字段中按Enter键后,上面的代码将恢复为默认的选择颜色.这是避免这种情况的一种方法.

The above code falls back to the default selection color once you press Enter in the textfield. Here's a way to avoid that.

@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

推荐答案

为什么不只设置文本字段的

Why not just set the properties of the text field's field editor directly when the field becomes first responder?

在典型的文本字段中,仅当该字段是第一响应者时,选择才可见,因此,如果您请求文本字段的字段编辑器,然后设置个选定的文本属性,则可以同样的影响,不是吗?

In a typical text field, selection is only visible when the field is first responder, so if you ask for the text field's field editor, then set its selected text attributes, you'd get the same affect, wouldn't you?

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的字段编辑器方法,则需要将其强制转换为NSTextView,以使编译器的警告静音.如果将来可以更正方法原型,则强制转换不会破坏代码中的任何内容,因此可以安全地保留在其位置.

Note: As discussed in the comments below, the documentation is correct in saying the field editor is an instance of NSTextView, but the -[NSWindow fieldEditor:forObject:] method claims to return an NSText (NSTextView's immediate superclass). If you plan to send the field editor NSTextView-only methods, you'll need to cast it as an NSTextView to quiet the compiler's warnings. The casting won't break anything in your code, should the method prototype be corrected in the future, so it can safely be left in place.

这篇关于用NSTextView伪造NSTextField以获得漂亮的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆