NSTextAttachment dynamic resize?

  • Has anyone out there tackled the annoying task of having an
    NSTextAttachment which resizes with the NSTextView containing it, who
    wishes to share their hard-earned wisdom?  (Specifically, I am looking
    at the case of having an attachment which is exactly the visible width
    of the NSTextView, regardless of what size the textview may currently
    be.  This must apply to only one specific type of attachment; others
    remain with their current behaviors.)

    I imagine there is a way to do this by subclassing out NSLayoutManager
    and iterating through any attachments of a specific subclass
    ('MyResizingTextAttachmentCell' or something), but I would prefer not
    to have to subclass the layout manager and iterate through this way if
    I can avoid it.  If I must, I must, but the iterate-and-resize seems
    an inefficient method at best.

    --
    Rachel 'Sparks' Blackman -- sysadmin, developer, mad scientist
    "If it is not broken, give me five minutes to redesign it!"
  • Rachel Blackman <seattlesparks...> wrote:

    > Has anyone out there tackled the annoying task of having an
    > NSTextAttachment which resizes with the NSTextView containing it, who
    > wishes to share their hard-earned wisdom?  (Specifically, I am looking
    > at the case of having an attachment which is exactly the visible width
    > of the NSTextView, regardless of what size the textview may currently
    > be.  This must apply to only one specific type of attachment; others
    > remain with their current behaviors.)

    Shouldn't it be sufficiant to override -(NSSize)cellSize in your
    NSTextAttachmentCell subclass and return whatever size you want it to
    have?  You'd have to make sure that the layout is invalidated whenever
    the desired sizes of your cells change, but if you want them to be the
    same width as the text view, the layout will be invalidated anyway when
    the text view frame size changes.

    (I'm not an expert at this stuff, so I might be missing something.)

    --
    Stefan Haller
    Ableton
    http://www.ableton.com/
  • On Dec 10, 2007, at 11:39 AM, Rachel Blackman wrote:

    > Has anyone out there tackled the annoying task of having an
    > NSTextAttachment which resizes with the NSTextView containing it,
    > who wishes to share their hard-earned wisdom?  (Specifically, I am
    > looking at the case of having an attachment which is exactly the
    > visible width of the NSTextView, regardless of what size the
    > textview may currently be.  This must apply to only one specific
    > type of attachment; others remain with their current behaviors.)
    >
    > I imagine there is a way to do this by subclassing out
    > NSLayoutManager and iterating through any attachments of a specific
    > subclass ('MyResizingTextAttachmentCell' or something), but I would
    > prefer not to have to subclass the layout manager and iterate
    > through this way if I can avoid it.  If I must, I must, but the
    > iterate-and-resize seems an inefficient method at best.

    You should be able to do this within NSTextAttachmentCell.  Here's
    some code from a tiny example I exhibited at WWDC one year, that might
    help you out.

    Douglas Davidson

    @interface Controller : NSObject {
        id myTextView;
    }

    - (void)insertAttachment:(id)sender;

    @end

    @interface MyAttachmentCell : NSTextAttachmentCell {
    }

    @end

    @implementation Controller

    - (void)insertAttachment:(id)sender {
        NSTextAttachment *attachment = [[NSTextAttachment alloc]
    initWithFileWrapper:nil];
        MyAttachmentCell *cell = [[MyAttachmentCell alloc] init];
        [attachment setAttachmentCell:cell];
        [myTextView insertText:[NSAttributedString
    attributedStringWithAttachment:attachment]];
        [cell release];
        [attachment release];
    }

    @end

    @implementation MyAttachmentCell

    - (NSRect)cellFrameForTextContainer:(NSTextContainer *)textContainer
    proposedLineFragment:(NSRect)lineFrag glyphPosition:(NSPoint)position
    characterIndex:(unsigned)charIndex {
        return NSMakeRect(0, 0, lineFrag.size.width, 1);
    }

    - (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
    characterIndex:(unsigned)charIndex layoutManager:(NSLayoutManager
    *)layoutManager {
        [[NSColor blackColor] set];
        NSRectFill(cellFrame);
    }

    @end
previous month december 2007 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31            
Go to today