Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 463195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T23:09:04+00:00 2026-05-12T23:09:04+00:00

I have a NSTextView that I want to display a horizontal scroll bar. Following

  • 0

I have a NSTextView that I want to display a horizontal scroll bar. Following some leads on the internet, I have most of it working, except that I am having problems with the vertical scroll bar.

What I have done is to find the width of the longest line (in pixels with the given font) and then I resize the NSTextContainer and the NSTextView appropriately. This way the horizontal scroll bar is representative of the width and scrolling to the right will scroll to the end of the longest line of text.

After doing this work, I noticed that my NSScrollView would show and hide the vertical scroll bar as I typed. I’ve ‘fixed’ this problem by setting autohidesScrollers to NO before the resize and then YES afterwards. However, there still remains another problem where, as I type, the vertical scrollbar thumb jumps to the top of the scrollbar and back to the proper place as I type. I type ‘a’ <space>, it jumps to the top, I press the <space> again and it jumps back to the proper location.

Any thoughts?

Here is some sample code:

- (CGFloat)longestLineOfText
{
    CGFloat longestLineOfText = 0.0;

    NSRange lineRange;

    NSString* theScriptText = [myTextView string];

    NSDictionary* attributesDict = [NSDictionary dictionaryWithObject:scriptFont forKey:NSFontAttributeName]; //scriptFont is a instance variable

    NSUInteger characterIndex = 0;
    NSUInteger stringLength = [theScriptText length];

    while (characterIndex < stringLength) {
        lineRange = [theScriptText lineRangeForRange:NSMakeRange(characterIndex, 0)];

        NSSize lineSize = [[theScriptText substringWithRange:lineRange] sizeWithAttributes:attributesDict];
        longestLineOfText = max(longestLineOfText, lineSize.width);

        characterIndex = NSMaxRange(lineRange);
    }

    return longestLineOfText;

}

// ----------------------------------------------------------------------------

- (void)updateMyTextViewWidth
{
    static CGFloat previousLongestLineOfText = 0.0;

    CGFloat currentLongestLineOfText = [self longestLineOfText];
    if (currentLongestLineOfText != previousLongestLineOfText) {
        BOOL shouldStopBlinkingScrollBar = (previousLongestLineOfText < currentLongestLineOfText);
        previousLongestLineOfText = currentLongestLineOfText;

        NSTextContainer* container = [myTextView textContainer];
        NSScrollView* scrollView = [myTextView enclosingScrollView];
        if (shouldStopBlinkingScrollBar) {
            [scrollView setAutohidesScrollers:NO];
        }

        CGFloat padding = [container lineFragmentPadding];

        NSSize size = [container containerSize];
        size.width = currentLongestLineOfText + padding * 2;
        [container setContainerSize:size];

        NSRect frame = [myTextView frame];
        frame.size.width = currentLongestLineOfText + padding * 2;
        [myTextView setFrame:frame];

        if (shouldStopBlinkingScrollBar) {
            [scrollView setAutohidesScrollers:YES];
        }
    }   
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T23:09:04+00:00Added an answer on May 12, 2026 at 11:09 pm

    Thanks to Ross Carter’s post on the Cocoa-Dev list, I resolved this issue.

    A. You have to set up your text view to support horizontal scrolling:

    - (void)awakeFromNib {
        [myTextView setHorizontallyResizable:YES];
        NSSize tcSize = [[myTextView textContainer] containerSize];
        tcSize.width = FLT_MAX;
        [[myTextView textContainer] setContainerSize:tcSize];
        [[myTextView textContainer] setWidthTracksTextView:NO];
    }
    

    B. You have to update the width of the text view as it changes, otherwise the horizontal scroll-bar doesn’t update properly:

    - (void)textDidChange:(NSNotification *)notification
    {
        [self updateTextViewWidth];
    }
    
    - (CGFloat)longestLineOfText
    {
        CGFloat longestLineOfText = 0.0;
    
        NSLayoutManager* layoutManager = [myTextView 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)updateTextViewWidth
    {
        static CGFloat previousLongestLineOfText = 0.0;
    
        CGFloat currentLongestLineOfText = [self longestLineOfText];
        if (currentLongestLineOfText != previousLongestLineOfText) {
            previousLongestLineOfText = currentLongestLineOfText;
    
            NSTextContainer* container = [myTextView textContainer];
            CGFloat padding = [container lineFragmentPadding];
    
            NSRect frame = [myTextView frame];
            frame.size.width = currentLongestLineOfText + padding * 2;
            [myTextView setFrame:frame];
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a small MacRuby app that displays some text inside a NSTextView .
I want to toggle rich text formatting in NSTextView. I have tried following: [contentView
I have a placeholder string, a space character, in an NSTextView that I want
My question is the following, I have an NSTextView and I want to do
I have a subclassed NSTextView that I am manipulating in a separate thread (using
I have a button that does something to the selected text in NSTextView. If
I have the following class hierarchy: NSViewController MyGeneralViewController ViewControllerA ViewControllerB MyGeneralViewController holds some common
I have an NSTextView that I'm outputting text from NSTask. Everything works as expected
(This question has been rewritten from an issue with NSTextView following some further research)
I have NSTextView that i add text to using def puts(val) storage = @output.textStorage

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.