简体   繁体   中英

Scrolling Content to Show Current Cursor Position on NSTextView

I have an NSTextView control object wrapped with NSScrollView . What I want to do is to make the text string at the current position visible when it's hidden below the content view.

在此处输入图片说明

In reference to the picture above, with

NSUInteger cPosition = [[[textView1 selectedRanges] objectAtIndex:0] rangeValue].location;
[textView1 scrollRangeToVisible:NSMakeRange(0,cPosition)];

the scroll view will scroll itself to a position such that the selected string (document) will come at the bottom of the content view. (Line 11). That's not exactly what I want. I would like the scroll view to scroll itself to show the text string at the current cursor position when it's hidden below the content view (for example, at Line 14). How can I improve my code?

Muchos thankos.

One solution would be to use NSString's enumerateSubstringsInRange:options:usingBlock: method with the NSStringEnumerationByParagraphs option and passing the range that contains your cPosition to scrollRangeToVisible:. This would have the effect of making the paragraph that contains your cPosition visible.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM