OSxTextAttachmentCell

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s