lucifer
lucifer

Reputation: 31

How to save text from textview?

I have a textview where you can write multiple lines of text. How do you save that text in a file or variables?

I used the multiline text field but it doesn't let me go to next line unless I hit control enter.

What I'm thinking is like a text editor, after you type everything you save that in a file. Or I can get each line from the text view into a variable.

What's the best way to do this?

Upvotes: 0

Views: 1752

Answers (2)

Magoo
Magoo

Reputation: 2619

NSString can save text up to 4.2 billion characters. \n denotes a line break, so no need to save into multiple parameters.

NSString *text = textView.text; 

OSX

NString *text = [[textView textStorage] string];

If you're looking for each individual line for whatever reason, you could use componentsSeparatedByString

NSArray *linesArray = [textView.text componentsSeparatedByString:@"\n"];

Each line will be available at linesArray[0], linesArray[1] etc... [linesArray count] will give you the total number of lines... with linesArray[[linesArray count]-1] being the last line in the string.

The textView.text property is an NSString also... so when you say saved.. do you mean intra app session? If so you can use NSUserDefaults

Save object

[[NSUserDefaults standardUserDefaults]setObject:textView.text forKey:@"TheKeyForMyText"];

Get object

NSString *text = [[NSUserDefaults standardUserDefaults]objectForKey:@"TheKeyForMyText"];

Upvotes: 2

Dhruv Khatri
Dhruv Khatri

Reputation: 813

Assign In swift

let var_name = textfield.text

Or in objective C

NSString *string_name = textfield.text;

And use the variable where you want to.

Upvotes: 1

Related Questions