1
votes

Suppose there is a window with has a NSTextView which contains enough text to trigger the scrollbars. When I resize the window, the textview is automatically scrolled so that the line which contains the cursor appears in the middle of the textview.

For example, this can also be seen in TextEdit in MacOS: paste bunch of text in it, scroll almost to the top [1], place cursor into the first visible line and resize the window. Now the view should scroll its content so that the cursor lands in the middle of the view.

My question is, how do I turn off this behavior? That is, I would like the textview to never automatically scroll the cursor to the middle when the window gets resized..?

[1] The actual scroll position at which the said behavior happens may require some trial-and-error, as I was unable to find out a pattern at which this happens. In my testing it happened when the scrollbar is at 10% - 30% position of the total height (from the top).

1
Do you want your textview to be editable?? - Hussain Shabbir
@HussainShabbir Yes. - Ecir Hana
@Eric refer the answer below - Hussain Shabbir

1 Answers

0
votes

You can do the tweak like this below:-

Create Custom Class of NSTextView and implement one delegate method for textview resizing and one method when click on textview. Refer below:-

.h file

@interface textView : NSTextView
@end

.m file

#import "textView.h"

@implementation textView

- (void)drawRect:(NSRect)dirtyRect {
    [super drawRect:dirtyRect];

    // Drawing code here.
}
//Below delegate method which will call when resize the textview. So just set your text view to be non editable.
- (void)viewDidEndLiveResize
{
    [self setEditable:NO];
    [self setSelectable:NO];
}

//Now when you click on the textview below method will called.
-(void)mouseDown:(NSEvent*) theEvent
{
    [super mouseDown:theEvent];
    [self setEditable:YES];
    [self setSelectable:YES];
}

-(void)keyDown:(NSEvent *)theEvent
{
    [super keyDown:theEvent];
    [self setEditable:YES];
    [self setSelectable:YES];
}

@end

Edit:- Also, mention the custom class name in interface builder inside textview -> Custom Class