Re: Convert NSArray to NSString
Re: Convert NSArray to NSString
- Subject: Re: Convert NSArray to NSString
- From: Ben Lachman <email@hidden>
- Date: Sat, 12 Apr 2008 23:59:41 -0400
I assume you mean selectedRanges, which returns an NSArray of
ranges. This array contains NSValue objects that represent ranges of
selected text, not actually the text that is selected. To get the
text that is selected you have to do a bit more work. First you need
to the get string of text from the textview. You can either do this
using [aTextView string] or extract a string from the textStorage
object that Michael mentioned. Then, for each NSValue in the array
you must retrieve an NSRange using rangeValue, and then call
substringWithRange: on the string you got in the first part. You can
do whatever you want with this string--append it to a mutable string
or just store it--just make sure you retain it if you plan on keeping
it around since it is an autoreleased object that is returned by
substringWithRange (ignore this is if you're using GC).
If you just want the full text you can just call [aTextView string].
selectedRange just returns a single range and so you would only have
to do the operation described above once, but may not get all the
text selected if the textview supports multiple selections. Make
sure you also check that ranges returns by selectedRange(s) doesn't
contain the NSNotFound marker as well.
->Ben
--
Ben Lachman
Acacia Tree Software
http://acaciatreesoftware.com
email@hidden
740.590.0009
On Apr 12, 2008, at 8:49 PM, Michael Vannorsdel wrote:
The textStorage of the NSTextView is actually an
NSMutableAttributedString subclass so you can treat it as such and
get the text and attributes from it.
On Apr 12, 2008, at 6:48 PM, Lincoln Green wrote:
I am trying to store the selected text in an NSTextView in an
NSString/NSMutableString. I have found the selectedRange method,
but can't figure out how to turn the NSArray into an NSString. Any
help would be greatly appreciated. Thanks!
_______________________________________________
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