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が以下に提案するソリューション
ジョシュアのおかげで、これは私が探していたものに対する素晴らしい解決策です:
@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