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:
After editing all goes back to a thinner string:
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];
}