On Sep 11, 2009, at 13:36, Ross Carter wrote:

It looks like you instantiate the NSTextView in the nib. I would start by setting up the NSTextView manually and adding it as a subview to your FSEmbeddedTextView2 instance. Make sure that the textView has its resizing behavior set up correctly (vertical, horizontal, both, depending on your needs). Next, I would make sure that you are getting the frame changed notification and that it is firing embeddedTextViewFrameDidChange:.

Hi Ross,

This all worked and was double checked before I made my original post...

I have investigated this further, and came up with an implementation that works flawlessly, as far as I have tested it. It produces the desired resizing when editing text, as well as when resizing the ancestor scroll view (typically as a consequence of window resizing). It is based on the manipulation of the textViews self-resizing flags at appropriate times. Here is the implementation, in case anyone is interested:

Oh, there is no documentation so let me point something out. This implementation works under the assumption that the FSEmbeddedText (the name sucks, I have yet to figure out an appropriate name for this class) is contained in an NSScrollView, directly. For it to work, the textView outlet needs to be connected to an NSTextView that is a descendant view of the FSEmbeddedTextView. I believe it does not have to be a direct subview, but I have not tested this assumption yet...



@interface FSEmbeddedTextView : NSView {

        IBOutlet NSTextView* textView;
        NSRect textViewFrame;
        BOOL isAutosizing;
        NSScrollView* scrollViewAncestor;
}

@end


@implementation FSEmbeddedTextView

-(void)awakeFromNib
{
        // find the scroll view ancestor and keep a reference
for(NSView* superview = [self superview] ; superview != nil ; superview = [superview superview])
                if([superview isKindOfClass:[NSScrollView class]]){
                        scrollViewAncestor = (NSScrollView*)superview;
                        break;
                }
        
        if(textView != nil){
                
                // track the frame of the text view
                textViewFrame = [textView frame];
                isAutosizing = NO;
                
                // observe notifications of the text view resizing
                [textView setPostsFrameChangedNotifications:YES];
                NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
                [nc addObserver:self
                           selector:@selector(embeddedTextViewFrameDidChange:)
                                   name:NSViewFrameDidChangeNotification
                                 object:textView];
        }
}

-(void)resizeSubviewsWithOldSize:(NSSize)oldBoundsSize
{
        // first turn off autoresizing on the text view
// this will make the NSTextView resize itself in the standard NSView fashion
        [textView setHorizontallyResizable:NO];
        [textView setVerticallyResizable:NO];
        
        // then do the resize
        isAutosizing = YES;
        [super resizeSubviewsWithOldSize:oldBoundsSize];
        isAutosizing = NO;
        
        // now turn the resizing back on
        [textView setHorizontallyResizable:NO];
        [textView setVerticallyResizable:YES];
        
        // and make the text view resize to fit
        [textView sizeToFit];
}

-(void)embeddedTextViewFrameDidChange:(NSNotification*)n
{
        // only react to this if this view is not currently autosizing
        if(!isAutosizing){
                
                // find out the diff between the old text view size
                // and the new size
                NSRect newTextFrame = [textView frame];
                float increaseX = newTextFrame.size.width - 
textViewFrame.size.width;
float increaseY = newTextFrame.size.height - textViewFrame.size.height;
                
                // revert the text view size to the size it has before this 
resize
                [textView setFrame:textViewFrame];
                
                // resize self to accomodate for the size of the text view
                NSRect newSelfFrame = [self frame];
                newSelfFrame.size.width += increaseX;
                newSelfFrame.size.height += increaseY;
                
// the minimum size to resize self to is the contentSize of the scroll view
                NSSize scrollViewContentSize = [scrollViewAncestor contentSize];
                if(newSelfFrame.size.width < scrollViewContentSize.width)
                        newSelfFrame.size.width = scrollViewContentSize.width;
                if(newSelfFrame.size.height < scrollViewContentSize.height)
                        newSelfFrame.size.height = scrollViewContentSize.height;
                
                [self setFrameSize:newSelfFrame.size];
                [self setNeedsDisplay:YES];
        }
                
        // update the text view frame we are keeping track of
        textViewFrame = [textView frame];       
}

-(void)dealloc
{
        if(textView != nil){
                NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
[nc removeObserver:self name:NSViewFrameDidChangeNotification object:textView];
        }
        
        [super dealloc];
}

@end
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to