Re: NSTextAttachment dynamic resize?
Re: NSTextAttachment dynamic resize?
- Subject: Re: NSTextAttachment dynamic resize?
- From: Douglas Davidson <email@hidden>
- Date: Wed, 12 Dec 2007 18:05:24 -0800
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
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden