1

This one has been killing me for some time. I have a subclassed NSTextField with:

- (void)drawRect:(NSRect)dirtyRect {
    if ([(SFIDisk *)self.cell.representedObject label]) {
        // Calculate text width to draw label color around the text.
        CGFloat textWidth = self.attributedStringValue.size.width + kSFIDiskViewTitleTextFieldCellMarginForTextSize;
        CGFloat textStartX = dirtyRect.origin.x + ((dirtyRect.size.width - textWidth) * .5);

       // Draw label background.
       [SFIStyleKit drawDiskLabelWithFrame:NSMakeRect(textStartX, dirtyRect.origin.y, textWidth, dirtyRect.size.height)];
    }

    // Calculate text width to draw label color around the text.
    CGFloat textStartY = dirtyRect.origin.y + ((dirtyRect.size.height - self.attributedStringValue.size.height) * .5);
    CGFloat textWidth = self.attributedStringValue.size.width;
    CGFloat textStartX = dirtyRect.origin.x + ((dirtyRect.size.width - textWidth) * .5);
    // Due to the icon in attribtued string, center drawing for string needs to be manually done.
    if (!self.currentEditor) {
        // Since drawing happens in CALayer and it cannot support font smoothing, this feature needs to be turned off in order to work.
        // https://stackoverflow.com/questions/715750/ugly-looking-text-when-drawing-nsattributedstring-in-cgcontext
        CGContextSetShouldSmoothFonts((CGContextRef)[[NSGraphicsContext currentContext] graphicsPort], NO);
        [self.attributedStringValue drawAtPoint:NSMakePoint(textStartX, textStartY)];
    }
}

This seems to works perfect in the UI:

But when editing the NSTextView draws the string a bit heavier:

enter image description here

After editing all goes back to a thinner string:

enter image description here

Goal is to have both looking the same, so am assuming something is wrong in my draw string call since the editor is not manipulated by me.

I cannot figure this out for the life of me. Any ideas?

EDIT: Did some extra tests by manually setting font for both NSTextField and its corresponding currentEditor when editing. For every weight set for the font, currentEditor always displays the next weight level.

By finding this out i did a workaround but a proper answer to this is still needed.

Work around code

if ([(SFIDisk *)self.cell.representedObject label]) {

    ....

    self.currentEditor.font = [NSFont systemFontOfSize:13 weight:NSFontWeightLight];
}
TtheT
  • 96
  • 7

1 Answers1

0

While you're editing, you'll be looking at the field editor, which is a NSTextView, rather than your NSTextField. Therefore, your override of -drawRect won't come into play until editing ends and the field editor is hidden again.

Charles Srstka
  • 16,665
  • 3
  • 34
  • 60
  • You are correct, but the question at hand already takes that into account. I've edited to make it more clear. – TtheT Aug 24 '17 at 23:34
  • I don't see any mention of NSTextView or the field editor in your question, only an override of -drawRect: which would not be used when drawing the field editor. – Charles Srstka Aug 24 '17 at 23:37
  • my bad, to me it was implicit, have updated question to reflect this. – TtheT Aug 25 '17 at 07:37