Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the text in an NSTextView?

I need to find the text in an NSTextView, and save it to a file. I can do the saving fine. I have used -stringValue, and -textStorage so far, but neither have worked. When I put -stringValue in, it just gave me (null), and when I put -textStorage in, it gave me a very large chunk of nothing (like a long paragraph of invisible text).

How can I put the text from an NSTextView into an NSString?

like image 718
Justin Avatar asked Apr 03 '11 21:04

Justin


3 Answers

Try

NSString *text = [[myTextView textStorage] string];

The NSTextStorage inherits from NSMutableAttributedString which inherits from NSAttributedString. The latter implements the string method.

This isn't too obvious if you just look at the NSTextView's instance methods in the documentation.

like image 114
Nick Weaver Avatar answered Nov 15 '22 11:11

Nick Weaver


Try this:

[myTextView string];

If you are struggling to write the textView stringValue into a file try something like this:

    [[myTextView string] writeToFile:@"someFile" atomically:YES encoding:NSUnicodeStringEncoding error:&error];

Hope this helps!

like image 3
Johann Dirdal Avatar answered Nov 15 '22 13:11

Johann Dirdal


when setting value, use text.string = @"XXXX";
when getting value, use STRING = [text.string copy];

because if you forget copy, the mutableString will be deliver to STRING. This will raise bugs when you editing multiple things with a single textview.

like image 1
Henry Sou Avatar answered Nov 15 '22 11:11

Henry Sou