TextView : Shifting all text down to make room for a subview
TextView : Shifting all text down to make room for a subview
- Subject: TextView : Shifting all text down to make room for a subview
- From: Seth Willits <email@hidden>
- Date: Wed, 01 Apr 2015 14:19:06 -0700
I have a text view where I added a subview at the top, and I want all of the text to be below this subview. You can think of it like having a horizontal ruler above the text view, but instead I want this view (it's not a ruler) _in_ the text view so that it scrolls with the text.
Here are two different strategies, neither of which I can quite get to work...
--
The simplest thing I could think of was to subclass NSTextView and override textContainerOrigin to push the Y value down a little. My little accessory view then becomes a direct subview of the text view. The first line of text is exactly in the right spot so it seems like it'll work perfectly, but if the text fills the entire text view, it won't start scrolling until the height of the *text* is greater than the height of the entire text *view*, which means that however many points I've shifted the text down by, that many points of text is cut off at the bottom of the text view before scrolling is allowed.
In other words, if the text view's could hold 10 lines of text, I shift all the text down by 1 line, and put 10 lines of text into the text view, I expect to see 9 lines and have to scroll to see the 10th, but instead, the scrollview doesn't allow scrolling at all, so that 10th line is completely inaccessible. If I add an 11th line, then I can scroll to the 10th line, but not the 11th, etc.
So whatever mechanism is calculating how much scrolling is needed, doesn't respect the text container's origin as returned by NSTextView -textContainerOrigin? I can't seem to figure out how to "fix" that.
---
A completely different approach would be to affect typesetting where the line fragments "flow" around this view, by just making sure the line fragments don't start until below it. I implemented this strategy with a custom text container, and it appears to work at first, but if the text is long enough that scrolling is required, then the first line fragment is just at 0,0 in the text container regardless of the fact that I explicitly told it to not be...
@implementation MyTextContainer
- (BOOL)isSimpleRectangularTextContainer
{
return NO;
}
- (NSRect)lineFragmentRectForProposedRect:(NSRect)proposedRect sweepDirection:(NSLineSweepDirection)sweepDirection movementDirection:(NSLineMovementDirection)movementDirection remainingRect:(NSRectPointer)remainingRect
{
if (proposedRect.origin.y + proposedRect.size.height > self.containerSize.height) {
return [super lineFragmentRectForProposedRect:proposedRect sweepDirection:sweepDirection movementDirection:movementDirection remainingRect:remainingRect];
}
if (!NSIntersectsRect(NSMakeRect(0, 0, self.containerSize.width, 26), proposedRect)) {
return [super lineFragmentRectForProposedRect:proposedRect sweepDirection:sweepDirection movementDirection:movementDirection remainingRect:remainingRect];
}
NSRect reproposedRect;
reproposedRect.origin.x = proposedRect.origin.x;
reproposedRect.size.height = proposedRect.size.height;
reproposedRect.size.width = self.containerSize.width;
reproposedRect.origin.y = 26;
reproposedRect = [self lineFragmentRectForProposedRect:reproposedRect sweepDirection:sweepDirection movementDirection:movementDirection remainingRect:remainingRect];
return reproposedRect;
}
@end
So there are two different strategies, and I've hit a sizable enough wall that with both that I'm kinda stumped.
Anyone had any luck doing something like this?
--
Seth Willits
_______________________________________________
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