2013-09-02 24 views
9

NSTextField nesnesinin kenarlık rengini değiştirmek istiyorum ancak bunu başaramıyorum.NSTextField öğesinin kenarlık rengini değiştir

Zaten birçok çözüm EX denedi: arka plan çizmek, alt sınıf olmak ...

herhangi bir fikir bu sorunu çözmek veya paylaşabilirsiniz birisi var mı?

Lütfen bana bildirin. Çok teşekkürler.

+0

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

cevap

13

Kullanım 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]; 
    } 
} 

Çıktı:

enter image description here

enter image description here

+0

@AviramNetanel kenarlığı ve arka planı aynı değil. –

-2

benim çözüm oldu:

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

ve geri ile ayarlayabilirsiniz:

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

Çözümünüz sınır için değil. –

+0

@ParagBafna - Biliyorum. ama aynı zamanda sınırı da değiştiriyor, bu yüzden benim için yeterince iyiydi. –

0

Benim için Parag cevabı çizim bazı garip textfield sonuçlandı, bu yüzden bu basit kod ile sona erdi (onun Yanıta göre):

- (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]; 
} 
İlgili konular