How to get the selected line range of NSTextView
?
相关问题
- CALayer - backgroundColor flipped?
- Core Data lightweight migration crashes after App
- back button text does not change
- iOS (objective-c) compression_decode_buffer() retu
- how to find the index position of the ARRAY Where
相关文章
- 现在使用swift开发ios应用好还是swift?
- Visual Studio Code, MAC OS X, OmniSharp server is
- TCC __TCCAccessRequest_block_invoke
- xcode 4 garbage collection removed?
- IntelliJ IDEA can't open projects or add SDK o
- Automator: How do I use the Choose from List actio
- Unable to process app at this time due to a genera
- How can I add media attachments to my push notific
Have a look at the NSTextView documentation, there is a whole section devoted to dealing with text selections:
Such as
selectedRanges
An outline algorithm for you:
selectedRange
lineRangeForRange
to obtain a range for the characters making up the line the last char of the selection is in.lineRangeForRange
to find the range for the preceding line. Repeat this process until you reach the start of the text. You'll have the line number of the last character in the original selection.Of course you could work the other way around - start with the line range for the first char in the text and work forward. For every line checking whether the start/end of the selection is in that line, stopping when you've found the line containing the end of the selection.
For code which does the reverse - given a range of lines it produces a selection to cover them - see Apple's TextEdit code sample, look at
LinePanelController.m
. Though this is doing the opposite of what you want reading it will show how the above mentioned methods work.HTH.
First, get the selected range through
[textView selectedRange]
Then you can get the line range through
- (NSRange)lineRangeForRange:(NSRange)range
of[textView string]