0

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?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
lucifer
  • 31
  • 5
  • I haven't set up code because I don't know what to use. Can you get text from a textview? Or do I have to set up a textfield. Right now I just have a text view in a viewcontroller. – lucifer May 16 '16 at 04:04

2 Answers2

2

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"];
Magoo
  • 2,552
  • 1
  • 23
  • 43
1

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.

maddy
  • 4,001
  • 8
  • 42
  • 65
Dhruv Khatri
  • 803
  • 6
  • 15