12
votes

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.

4

4 Answers

14
votes

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:

enter image description here

enter image description here

5
votes

You can try CALayer without the need for subclassing

self.wantsLayer = true
self.layer?.borderColor = NSColor.red.cgColor
self.layer?.borderWidth = 1
0
votes

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];
}
0
votes

SWift 5 version of Parag's answer but adds ability for user to define title and border color.

fileprivate class URLField: NSTextField {
    var title : String?
    var borderColor: NSColor?

    override func mouseDown(with event: NSEvent) {
        super.mouseDown(with: event)
        if let textEditor = currentEditor() {
            textEditor.selectAll(self)
        }
    }

    convenience init(withValue: String?, modalTitle: String?) {
        self.init()

        if let string = withValue {
            self.stringValue = string
        }
        if let title = modalTitle {
            self.title = title
        }
        self.cell?.controlView?.wantsLayer = true
        self.cell?.controlView?.layer?.borderWidth = 1
        self.lineBreakMode = .byTruncatingHead
        self.usesSingleLineMode = true
    }

    override func draw(_ dirtyRect: NSRect) {
        super.draw(dirtyRect)

        if let color = borderColor {
            ///self.layer?.borderColor = color.cgColor
            let path = NSBezierPath.init(rect: frame)
            path.lineWidth = 1
            color.setStroke()
            path.stroke()

            if self.window?.firstResponder == self.currentEditor() && NSApp.isActive {
                NSGraphicsContext.saveGraphicsState()
                NSFocusRingPlacement.only.set()
                NSGraphicsContext.restoreGraphicsState()
            }
        }
    }

    override func viewDidMoveToWindow() {
        super.viewDidMoveToWindow()

        if let title = self.title {
            self.window?.title = title
        }

        // MARK: this gets us focus even when modal
        self.becomeFirstResponder()
    }
}