Re: Adding multiple NSTextViews inside one NSScrollView
Re: Adding multiple NSTextViews inside one NSScrollView
- Subject: Re: Adding multiple NSTextViews inside one NSScrollView
- From: Ross Carter <email@hidden>
- Date: Mon, 16 Mar 2009 13:52:54 -0400
On Mar 16, 2009, at 10:39 AM, Kyle Sluder wrote:
On Mon, Mar 16, 2009 at 9:17 AM, Ashish Tiwari
<email@hidden> wrote:
The behavior I want is that when user scrolls the shared ScrollView
all 3
textviews should scroll simultaneously.
Ah, is this kind of like a 3-way diff? I had originally envisioned
something like a three-column document layout, almost like a
newspaper.
If it better models your user's mental model, that's all well and good
to have three separate text views. They definitely can't belong to
the same scroll view, however. You'll have to use separate scroll
views and synchronize them.
If I understand the question correctly, the OP wants one scroll view
to contain 3 text views. The text views have separate layout managers
and are vertically aligned at the top of the scroll view.
Here's an NSScrollView subclass that contains and scrolls three side-
by-side NSTextViews.
@interface ThreeScrollView : NSScrollView {
NSTextView* tv1;
NSTextView* tv2;
NSTextView* tv3;
BOOL isAdjustingFrames;
}
@end
@implementation ThreeScrollView
- (BOOL)isFlipped { return YES; }
- (void)awakeFromNib {
// add the NSTextViews programmatically; I don't know whether this is
possible in IB:
NSView *docView = [self documentView];
NSRect docViewFrame = [docView frame];
CGFloat tvWidth = 100.0;
NSRect tv1Frame = docViewFrame;
tv1Frame.size.width = tvWidth;
NSRect tv2Frame = tv1Frame;
tv2Frame.origin.x = 125.0;
NSRect tv3Frame = tv2Frame;
tv3Frame.origin.x = 250.0;
tv1 = [[NSTextView alloc] initWithFrame:tv1Frame];
[tv1 setVerticallyResizable:YES];
tv2 = [[NSTextView alloc] initWithFrame:tv2Frame];
[tv2 setVerticallyResizable:YES];
tv3 = [[NSTextView alloc] initWithFrame:tv3Frame];
[tv3 setVerticallyResizable:YES];
[docView addSubview:tv1];
[docView addSubview:tv2];
[docView addSubview:tv3];
// listen for frame changed notifications:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(textViewChangedFrame:)
name:NSViewFrameDidChangeNotification object:tv1];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(textViewChangedFrame:)
name:NSViewFrameDidChangeNotification object:tv2];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(textViewChangedFrame:)
name:NSViewFrameDidChangeNotification object:tv3];
}
- (void)textViewChangedFrame:(NSNotification *)note {
// set the height of the text views and the documentView to the
tallest text view height:
if (isAdjustingFrames) return;
isAdjustingFrames = YES;
NSRect tv1Frame = [tv1 frame];
NSRect tv2Frame = [tv2 frame];
NSRect tv3Frame = [tv3 frame];
CGFloat maxHeight = NSHeight(tv1Frame);
if (NSHeight(tv2Frame) > maxHeight) maxHeight = NSHeight(tv2Frame);
if (NSHeight(tv3Frame) > maxHeight) maxHeight = NSHeight(tv3Frame);
tv1Frame.origin.y = 0;
tv1Frame.size.height = maxHeight;
tv2Frame.origin.y = 0;
tv2Frame.size.height = maxHeight;
tv3Frame.origin.y = 0;
tv3Frame.size.height = maxHeight;
NSView *docView = [self documentView];
NSRect docViewFrame = [docView frame];
docViewFrame.size.height = maxHeight;
[docView setFrame:docViewFrame];
[tv1 setFrame:tv1Frame];
[tv2 setFrame:tv2Frame];
[tv3 setFrame:tv3Frame];
isAdjustingFrames = NO;
}
@end
Ross
_______________________________________________
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