2013-09-02 22 views
9

NSTextFieldオブジェクトの境界線の色を変更したいが、それを達成できない。NSTextFieldの境界線の色を変更する

私はすでに多くのソリューションにEXを試してみました:

は、この問題を解決するか、任意のアイデアを共有することができ、誰もがそこにある...背景を描き、サブクラスで?

お知らせください。どうもありがとう。

+0

ます。https: //developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSBezierPath_Class/Reference/Reference.html –

答えて

13

使用NSBezierPath

- (void)drawRect:(NSRect)dirtyRect 
{ 
    NSPoint origin = { 0.0,0.0 }; 
    NSRect rect; 
    rect.origin = origin; 
    rect.size.width = [self bounds].size.width; 
    rect.size.height = [self bounds].size.height; 

    NSBezierPath * path; 
    path = [NSBezierPath bezierPathWithRect:rect]; 
    [path setLineWidth:2]; 
    [[NSColor colorWithCalibratedWhite:1.0 alpha:0.394] set]; 
    [path fill]; 
    [[NSColor redColor] set]; 
    [path stroke]; 

    if (([[self window] firstResponder] == [self currentEditor]) && [NSApp isActive]) 
    { 
     [NSGraphicsContext saveGraphicsState]; 
     NSSetFocusRingStyle(NSFocusRingOnly); 
     [path fill]; 
     [NSGraphicsContext restoreGraphicsState]; 
    } 
    else 
    { 
     [[self attributedStringValue] drawInRect:rect]; 
    } 
} 

出力:

enter image description here

enter image description here

+0

@AviramNetanelのボーダーと背景は同じではありません。 –

-2

私の解決策だった:

[self.txtfield setBackgroundColor:[[NSColor redColor] colorWithAlphaComponent:0.5]]; 

あなたと戻ってそれを設定することができます。

[self.txtfield setBackgroundColor:[[NSColor grayColor] colorWithAlphaComponent:0.5]]; 
+0

あなたの解は国境のためではありません。 –

+0

@ParagBafna - わかっています。しかしそれはまた国境を変えるので、私にとっては十分だった。 –

0

私にとってParagの答えは描画いくつかの奇妙なテキストフィールドになったので、私は(彼の答えに基づいて)この単純なコードになってしまった:

- (void)drawRect:(NSRect)dirtyRect { 
    [super drawRect:dirtyRect]; 

    if (!self.borderColor) { 
     return; 
    } 

    NSPoint origin = { 0.0,0.0 }; 
    NSRect rect; 
    rect.origin = origin; 
    rect.size.width = [self bounds].size.width; 
    rect.size.height = [self bounds].size.height; 

    NSBezierPath * path; 
    path = [NSBezierPath bezierPathWithRect:rect]; 
    [path setLineWidth:2]; 
    [self.borderColor set]; 
    [path stroke]; 
} 
関連する問題