1
votes

I'm porting an iPhone app that uses this tutorial into a Mac app. I've been able to get text to display in NSScrollView, but I can't get the text to scroll and I hope someone knows what I am missing.

In the iPhone app, CTView is subclassed from UIScrollView

CTView.h:

@interface CTView : UIScrollView <UIScrollViewDelegate> {

1) But there is no NSScrollViewDelegate in AppKit. Is the missing delegate interface to NSScrollView the problem?

So for the Mac app, CTView.h looks like the following:

@interface CTView : NSScrollView 

In CTView.m I have

[self setHasHorizontalScroller:YES];
[self setAcceptsTouchEvents:YES];

Also inside CTView.m I create a NSMutableArray of frames as

self.frames = [NSMutableArray array];

and fill the array up with frames. I also set the frame for the document view for the complete larger image that I want to be able to scroll through as:

[[self contentView] setFrame:[self bounds]];
[self.documentView setFrame:  NSMakeRect(0, 0, totalPages * self.bounds.size.width, textFrame.size.height)];

The first frame is visible in the CTView, but when I move the horizontal scroll bar, the second frame doesn't show up in CTView.

I'm using Xcode 4.3.3 and Mac OS X 10.7.4.

Does anyone know what I need to do differently from UIScrollView for NSScrollView to be able to scroll through the frames?

1

1 Answers

1
votes

Finally figured it out. Whew!

1) NSScrollView needs a view for its documentView, where the documentView is what is scrolled over. So in the nib I created a NSScrollView that contained a view for the documentView and set the documentView to that view as shown by the code in CTView.m

NSArray *viewArray     = [self subviews];
NSArray *docViewArray  = [[viewArray objectAtIndex:0] subviews]; 
NSView  *docView       = [docViewArray objectAtIndex:0];

[self setDocumentView:docView];

2) Then I added the array of frames, that would normally be added to UIScrollView, to the documentView of NSScrollView. The array of frames consists of multiple elements of content.

 while (textPos < [attString length]) { 
    CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(textPos, 0), innerPath, NULL);
    CFRange frameRange = CTFrameGetVisibleStringRange(frame); 

   CTColumnView* content = [[[CTColumnView alloc] initWithFrame: NSMakeRect(0, 0, self.contentSize.width, self.contentSize.height)] autorelease];
    [docView addSubview: content];  // <<-- Here is where the frames are added to the documentView
    textPos += frameRange.length;
  }

And that did the trick.