0

I'm failing to have a NSTextFieldCell created and drawn programmatically recreate the same visual appearance equivalently-configured NSTextFieldCells have when built in Interface Builder and drawn automatically by Cocoa.

Context is a custom NSView that displays content that user can select, delete or replace. Though this content is non-textual, I'd like the field to resemble an NSTextField, so I'm trying to press an NSTextFieldCell into service to draw a bezel, background fill, etc., that would match a corresponding NSTextField's.

I setup the cell as follows (during custom control init()):

textFieldCell = [[NSTextFieldCell alloc] init];
[textFieldCell setDrawsBackground: YES];
[textFieldCell setBackgroundColor: NSColor.blueColor];
[textFieldCell setBackgroundStyle: NSBackgroundStyleNormal];
[textFieldCell setBezeled: YES];
[textFieldCell setStringValue: @""];

and then in custom control's drawRect,

- (void) drawRect: (NSRect) rect
{
    // Draw an NSTextField-like background
    if( isSelectAndDeletable)
    {
         [textFieldCell drawWithFrame: [self bounds] inView: self];
    }
    ...  // more content drawing here
 }

This produces on onscreen 1-pixel frame of the appropriate thickness and size, and fills it with blue. But the frame itself is gray, not blue, and the fill touches the frame on top (no white margin) and misses it by two pixels at the bottom (thick white margin). By contrast, when I let Cocoa draw my textFieldCells either as part of an NSTextField or as standalone cells created in Interface Builder, the frame is drawn WITH the cell's background color and the fill is inset with one pixel white margin all around.

This picture demonstrates the problem. It shows three NSTextFieldCells, the second of which is (defectively) drawn by my own call to drawWithFrame:inView:, the other two (correctly) directly by the runtime.

Any ideas what I'm doing wrong? I feel like I am passing the proper rect (the frame is in the correct location), and the single call produces both the correct stroke (frame) and incorrect fill (background). Is this some misconfigured bezel effect? I can hand draw the effect I'm after but would rather stick with NSTextFieldCell if I can fix my approach here.

  • `NSTextFieldCell` objects are very fussy and carry a lot of legacy baggage—I'm just saying. First, there are a several properties you didn't set (`interiorBackgroundStyle`, `bordered`, etc.) that could affect drawing. My suggestion would be to start over using `[[NSTextFieldCell alloc] initTextCell:@""]` as that will likely get you closer to the text-cell-style you're looking for. – James Bucanek Nov 27 '18 at 17:25
  • Thanks. I've tried your suggestion but didn't get far. Setting isBordered is incompatible with setting isBezeled, and interiorBackgroundStyle is read-only so overrideable by subclass (where I want is to retain the appearance of the unsubclassed NSTextFieldCell). I _did_ discover when I turn off the Bezel, my fill is no longer incorrectly asymmetric top/bottom, but is now too large (fills whole frame, no single white pixel inset). But I still get an incorrectly colored frame (whether BORDER is YES or NO, or I'm bezeled) and never the right fill dimensions. – Nicholas Jackiw Nov 28 '18 at 07:52
  • Perhaps the right-colored frame and the single pixel white frame inset are actually properties of the NSTextField and not actually of the cell? At any rate, based on your suggestion of general fussiness, I think I should retreat to hand-drawing the appearance I want rather than try to resurrect zombie fragments of a full NSTextField+NSTextField view appearance! – Nicholas Jackiw Nov 28 '18 at 07:54
  • Good luck either way. I have a couple of `NSTextFieldCell` subclasses that I *have* to use (because I still have an `NSBrowser` view to maintain) and I literally want to cry whenever I need to change/fix something with ti. For example, try replacing the background color of the cell with a pattern. It sounds simple, but it's ridiculously convoluted because the background is drawn differently depending on the state of the cell (select, enabled, active, ...). – James Bucanek Nov 28 '18 at 16:44

0 Answers0