I want to change the border color of NSTextField object, but I can't achieve it.
I already have tried many solutions EX: be subclass, draw background...
is there anyone who can resolve this issue or share any ideas?
Please let me know. Many thanks.
Use 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];
}
}
Output:
For me Parag's answer resulted in some strange textfield drawing, so I ended up with this simple code (based on his answer):
- (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];
}