Re: How to stop NSScrollView from scrolling to top when horizontally resizing contained NSTextView?
Re: How to stop NSScrollView from scrolling to top when horizontally resizing contained NSTextView?
- Subject: Re: How to stop NSScrollView from scrolling to top when horizontally resizing contained NSTextView?
- From: Lyndsey Ferguson <email@hidden>
- Date: Wed, 18 Nov 2009 09:29:42 -0500
On Wed, Nov 18, 2009 at 12:22 AM, Ross Carter <email@hidden> wrote:
> You mean when wrapping is turned off? You get that behavior for free. Try this:
>
> 1. Create a new project in XCode.
> 2. In the app delegate header, set up an IBOutlet to a NSTextView.
> 3. In interface Builder, add a NSTextView to the window and connect it to the IBOutlet.
> 4. Add this to the app delegate .m :
>
> - (void)awakeFromNib {
> [myTextView setHorizontallyResizable:YES];
> NSSize tcSize = [[myTextView textContainer] containerSize];
> tcSize.width = FLT_MAX;
> [[myTextView textContainer] setContainerSize:tcSize];
> [[myTextView textContainer] setWidthTracksTextView:NO];
> }
>
> Run the app. As you add text to the text view, you will see that the horizontal scroller adjusts as needed to accommodate the longest line. You don't have to do anything more.
Hi Ross,
This _almost_ works for me except the horizontal scroll bar doesn't
change as I add more text if I've added around 100 characters to the
line. If I write more words at the end of the line of text, the
horizontal scroll-bar's thumb doesn't update to indicate that there is
more "width" to scroll and I cannot scroll to the end of the line.
However, you have given me a great tip and I believe that I have
solved the problem. As a delegate of the NSTextView, I receive
textDidChangeNotifications and I update the width of the NSTextView to
accommodate the widest length of text via my selector
"updateScriptViewWidth" (below). This works exactly as I wanted to
and there is no flickering. Let me know if you see any problem with
the below.
Thanks again Ross!
Lyndsey Ferguson
- (CGFloat)longestLineOfText
{
CGFloat longestLineOfText = 0.0;
NSLayoutManager* layoutManager = [scriptTextView layoutManager];
NSRange lineRange;
NSUInteger glyphIndex = 0;
NSUInteger glyphCount = [layoutManager numberOfGlyphs];
while (glyphIndex < glyphCount) {
NSRect lineRect = [layoutManager
lineFragmentUsedRectForGlyphAtIndex:glyphIndex
effectiveRange:&lineRange
withoutAdditionalLayout:YES];
longestLineOfText = max(longestLineOfText, lineRect.size.width);
glyphIndex = NSMaxRange(lineRange);
}
return longestLineOfText;
}
// ----------------------------------------------------------------------------
- (void)updateScriptViewWidth
{
static CGFloat previousLongestLineOfText = 0.0;
CGFloat currentLongestLineOfText = [self longestLineOfText];
if (currentLongestLineOfText != previousLongestLineOfText) {
previousLongestLineOfText = currentLongestLineOfText;
NSTextContainer* container = [scriptTextView textContainer];
CGFloat padding = [container lineFragmentPadding];
NSRect frame = [scriptTextView frame];
frame.size.width = currentLongestLineOfText + padding * 2;
[scriptTextView setFrame:frame];
}
}
--
Lyndsey Ferguson
_______________________________________________
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