How to save text from textview? - objective-c

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?

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"];

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.

Related

Keep text the same while keeping special characters

i have a text that contains end of lines; i would like to have that text introduced into a NSString and still recognize the end of line.
i.e. i don't want to have to place a "\n" at the end of every line.
how can i do so in Obj-c?
I think it's best to hold the text using an NSArray, each element of which is a separate line. You can use [NSString componentsSeparatedByCharactersInSet:] (reference) for that:
NSString *str = #"hello\nworld";
NSArray *lines = [str componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]];

Not showing smily ( Emoji ) in in UITextView in iOS?

I have stored all uni-codes(emoji characters) in plist supported by iphone. When i write directly as
- (IBAction)sendButtonSelected:(id)sender {
NSMutableArray *emoticonsArray = [[NSMutableArray alloc]initWithObjects:#"\ue415",nil];
NSString *imageNameToPass = [NSString stringWithFormat:#"%#",[emoticonsArray objectAtIndex:0]];
NSLog(#"imageNameToPass1...%#",imageNameToPass);
messageTextView.text =imageNameToPass;
}
it show emoji in textview but as soon as i fetch from plist
NSString *plistPath1 = [[NSBundle mainBundle] pathForResource:#"unicodes" ofType:#"plist"];
NSDictionary *dictionary = [[NSDictionary alloc] initWithContentsOfFile:plistPath1];
activeArray= [dictionary objectForKey:categoryString];
NSLog(#"activeArray...%#",activeArray);
emoticonsArrayForHomeEmoji = [[NSMutableArray alloc]initWithCapacity:[activeArray count]];
for(int i=0; i<[activeArray count]; i++)
{
id objects = (id)[activeArray objectAtIndex:i];
[emoticonsArrayForHomeEmoji insertObject:objects atIndex:i];
}
NSString *imageNameToPass = [NSString stringWithFormat:#"%#",[emoticonsArrayForHomeEmoji
objectAtIndex:0]];
NSLog(#"imageNameToPass1...%#",imageNameToPass);
messageTextView.text =imageNameToPass;
then it shows unicode as text \ue415 in text view instead of emoji.
What i am doing wrong?. Please help me out!
Wel said by #AliSoftware, the Plist data will be read as-it is, so you can add the emojis to your plist by following this steps:
1) Go to your top bar, and click on Edit.
2) Now select Special Characters
3) Now drag and drop emoji to plist.
For more details I am adding snap shots. take a look at it.
The \uxxxx notation is only interpreted by the compiler (as the source code is usually in ASCII or MacRoman or whatever but not often UTF8)
Plist files uses the characters directly, and are encoded in UTF8.
So you should insert the emoji character itself into the plist directly, instead of using the \uxxxx notation, as the Plist data will be read as-is.
Lion and Mountain Lion Keyboard palettes contains emoji characters directly, so that should not be difficult to insert the characters when editing the PLIST anyway.

\n does not skip to next line in NSString

NSMutableString *a = #"Hi";
NSMutableString *b =[a stringByAppendingString:#"\n\n Hi Again"];
The above doesn't give an error but does not put "Hi Again" on the next line. Why?
EDIT2
I realised after posting, that the OP had NSString in the title but put NSMutableString in the code. I have submitted an edit to change the NSMutableString to NSString.
I will leave this as it still maybe helpful.
Well I am surprised that does not give an error, because you are giving a NSMutableString a NSString.
You need to read the Documentation on NSMutableStrings.
to give you an idea
//non mutable strings
NSString *shortGreetingString = #"Hi";
NSString *longGreetingString = #"Hi Again";
/*mutable string - is created and given a character capacity The number of characters indicated by capacity is simply a hint to increase the efficiency of data storage. The value does not limit the length of the string
*/
NSMutableString *mutableString= [NSMutableString stringWithCapacity:15];
/*The mutableString, now uses an appendFormat to construct the string
each %# in the Parameters for the appendFormat is a place holder for values of NSStrings
listed in the order you want after the comma.
Any other charactars will be included in the construction, in this case the new lines.
*/
[mutableString appendFormat:#"%#\n\n%#",shortGreetingString,longGreetingString];
NSLog (#"mutableString = %#" ,mutableString);
[pool drain];
I think this might help you. You'd rather to use '\r' instead of '\n'
I also had a similar problem and found \n works in LLDB but not in GDB
Try using NSString. You could use:
NSString *a = [NSString stringWithFormat:#"%#\n\n%#", #"Hi", #"Hello again"]
If your string is going in a UIView (e.g a UILabel), you also need to set the number of lines to 0
myView.numberOfLines=0;

Reading rtf information from entity entries to NSString

I have an entity (named Song) that one of the Attributes is binary data (a rtf field) that I use to to store chord charts or notes about a song. I need to be able to print this field along with the other string fields of this entity. I have tried every permutation of this I can think of:
NSAttributedString* myDataTry = [[[NSAttributedString alloc] initWithRTF:myData documentAttributes:NULL]autorelease];
When I run this through the debuger the summary for myDataTry reads "out of scope" until the next line break and then myDataTry reads nil.
What do I need to change to make this thing go?
The reference for initWithRTF:documentAttributes: reads:
Returns an initialized object, or nil if rtfData can’t be decoded.
Are you absolutely certain that your RTF data can be decoded?
Here is what worked:
NSString *aStr = [[NSString alloc] initWithData:myData encoding:NSASCIIStringEncoding];
NSRange r = [aStr rangeOfString:#"{"];
NSString *newAStr = [aStr substringFromIndex:r.location];
NSData *newMyData;
newMyData = [newAStr dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:1];
NSAttributedString* myDataTry = [[[NSAttributedString alloc] initWithRTF:newMyData documentAttributes:NULL]autorelease];
Now to insert that into the window with everything else! Thanks for your point in the right direction #ShaggyFrog

Concatenating a string with the value of a NSTextField results in odd symbols in Objective-C

I'm in my second day of learning Objective-C, Cocoa and IB. This is probably something really simple but I cannot work it out.
Basically, I have a form with a NSTextField, when the user types in this field and clicks an OK button the application will display an alert saying Hello followed by the value of text field.
It's all working apart from the string concatenation. I'm using the following code to concatenate the string "Hello" and the NSTextField value:
NSString *nameText = [NSString stringWithFormat:#"Hello %s", [nameTextField stringValue]];
When the user clicks the OK button an alert displays "Hello ‡√Ÿpˇ"!
Camsoft,
The NSString of Obj-C is an object, correct the format call with:
NSString *nameText = [NSString stringWithFormat:#"Hello %#",[nameTextField stringValue]];
Note the %# instead of %s .
Frank
stringValue returns NSString object and %s expects c-string parameter. Try to use %# instead:
NSString *nameText = [NSString stringWithFormat:#"Hello %#", [nameTextField stringValue]];