OSTextAttachment.h
@interface OSxTextAttachmentCell : NSTextAttachmentCell @end
OSTextAttachment.m
@implementation OSxTextAttachmentCell - (NSRect)cellFrameForTextContainer:(NSTextContainer *)textContainer proposedLineFragment:(NSRect)lineFrag glyphPosition:(NSPoint)position characterIndex:(NSUInteger)charIndex { float width = lineFrag.size.width; NSRect rect = [self scaleImageSizeToWidth:width]; return rect; } // Scale the image to fit the line width less 10% - (NSRect)scaleImageSizeToWidth:(float)width { float scalingFactor = 1.0; NSSize imageSize = [self.image size]; if (width < imageSize.width) scalingFactor = (width*0.9) / imageSize.width; NSRect rect = NSMakeRect(0, 0, imageSize.width * scalingFactor, imageSize.height * scalingFactor); return rect; } - (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)aView { if (self.image != nil) { [self.image drawInRect:cellFrame fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0 respectFlipped:YES hints:nil]; } } @end