Hi Ross,

Thanks for your reply,

On Sep 09, 2009, at 13:15, Ross Carter wrote:

I've googled for a solution, found nothing. I tried making it by observing the NSViewFrameChangedNotification of the NSTextView from it's parent, and resizing, but this is more tricky then I imagined.

I think you are on the right track. You might want to look at these threads:
http://www.cocoabuilder.com/archive/message/cocoa/2009/4/24/235290
http://www.cocoabuilder.com/archive/message/cocoa/2009/3/16/232453

I took a look. The code you posted in the second thread (having a switch to stop recursive resizing) makes sense. I was thinking along similar lines.

At the bottom I pasted my current code on this. There is a problem though, but one which seems to be connected to having an NSTextView not directly embedded in a scroll view, not so much with my code... In short, NSTextView does not react to the parent view resizing in the standard way (which is easily tested in IB). Perhaps it assumes it is directly embedded in a scroll view. This makes it hard to reconcile with what I am trying to do, even if my added code works OK.

Any thoughts?

As as side question, it seems that an NSTextView has some margins that separate the text from it's outer edges. This makes sense when the NSTextView is in an NSScrollView, but makes it difficult to align the text view when it is not. I can't find any documented API on controlling this. Is it possible?

I think you want to look at setLineFragmentPadding: in NSTextContainer.

That sounds like the right thing, thanks...
F



@interface FSEmbeddedTextView2 : NSView {

        IBOutlet NSTextView* textView;
        NSRect textViewFrame;
        BOOL isChangingFrameSize;
}

@end


@implementation FSEmbeddedTextView2

-(void)awakeFromNib
{
        NSNotificationCenter* nc = [NSNotificationCenter defaultCenter];
        textViewFrame = [textView frame];
        [nc addObserver:self
                   selector:@selector(embeddedTextViewFrameDidChange:)
                           name:NSViewFrameDidChangeNotification
                         object:textView];
}

-(void)embeddedTextViewFrameDidChange:(NSNotification*)n
{
// if the resize of the text view is happening during a resize of this view
        // then it is autosizing at work, and we can ignore it
        if(isChangingFrameSize){
                textViewFrame = [textView frame];
                return;
        }

        // figure out the size change in the text view          
        NSRect newTextFrame = [textView frame];
        float increaseX = newTextFrame.size.width - textViewFrame.size.width;
        float increaseY = newTextFrame.size.height - textViewFrame.size.height;
        
        // reset the text view to where we started
        [textView setFrame:textViewFrame];
        
        // resize this view, which will autosize the text view to the new size
        // which can accommodate all the text
        NSRect newSelfFrame = [self frame];
        newSelfFrame.size.width += increaseX;
        newSelfFrame.size.height += increaseY;
        
        [self setFrameSize:newSelfFrame.size];
}

-(void)setFrame:(NSRect)newFrame
{
        isChangingFrameSize = YES;
        [super setFrame:newFrame];
        isChangingFrameSize = NO;
}

-(void)setFrameSize:(NSSize)newSize
{
        isChangingFrameSize = YES;
        [super setFrameSize:newSize];
        isChangingFrameSize = NO;
}

-(void)dealloc
{
        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