\n is not working in plist? - iphone

I created array.plist file where I am storing key value pair
e.g
key = MY_FIRST_MESSAGE
Value = Welcome\n I am happy to serve you.
in xml format
Welcome\n I am happy to serve you.
I received this sting in dictionary and display in textview like below code
textView.text = [myDict objectForKey:MY_FIRST_MESSAGE];
But
I am not able to get newline. It should be look like below
Welcome
I am happy to serve you
But I got it like
Welcome\n I am happy to serve you
On textview view,
I debug this problem, my got string from plist is like Welcome\n I am happy to serve you.
Please suggest me how to remove \n from string which are from plist?
I tried \n, /n, /\n all combination.
Thanks
Manu

.plist files are XML, so you should be able to use an actual newline character rather than the string "\n".
Failing that:
string = [string stringByReplacingOccurrencesOfString:#"\\n" withString:#"\n"];

Related

How to set line linebreak when passing parameters to Facebook

I am passing parameters to Facebook using JSON concept.I am passing Location name and Date in that. After location name i want to display Date in new line.Can any one help me how to give line break. Here is the my code.
Thanks in Advance.
NSString *att=[NSString stringWithFormat:#"{\"name\":\"%#\","
"\"href\":\"%#\","
"\"media\":[{\"type\":\"image\","
"\"src\":\"%#\","
"\"href\":\"%#\"}],"
"\"properties\":{\"Location\":\"%#\"\" Date\":\"%#\"}}",eventTitle.text,href,imageSource,imageHref,eventLocation.text,eventDate.text];
Not really a Facebook question. But certainly HTML-escaped spaces won't work.
You're probably looking for an escaped newline, \n
In fact, you'll need to escape it twice: once for NSString and once for JSON, ending up \n in your code

Formatting string from plist dictionary with new lines

I have some strings stored in a plist dictionary, the values of which i would like to display in a textview.
the strings are stored with \n for new lines, however when i pull out the string, it shows \n characters as literals instead of converting them to new lines
Here is the code to pull out the value
self.directions.text = [NSString stringWithFormat:#"%#",[self.exerciseDetail objectForKey:DIRECTIONS_KEY]];
How can i fix it?
Stored strings: some text \n some more text \n some more text
The problem here is that the plist file is storing the string with "\" and "n" characters, rather than a single newline character. The best fix (in my opinion) would be to fix the plist file to have the correct characters. If you are using the plist editor built into Xcode, then hitting the Enter key moves you to the next field. To get around this, hold the Option key while typing Enter and a newline will be inserted.

UITableViewCell newline characters

Is there any way to get newlines to be converted to actual linebreaks in UITableViewCell?
Right now they show up as \r\n (they come from a sqlite3 db) and just get displayed as such.
If I search and replace \r\n with actual return characters then it works fine, but I'm wondering if three is a proper way to do this?
Please don't suggest using other View types, the app is basically 100% complete save for this last bug and I don't want to re-test everything.
thanks
T
edit: Solution in case anyone has a similar issue:
cellText = [cellText stringByReplacingOccurrencesOfString:#"\\r\\n" withString:#"\n"];
Olaf got me on the right path, thanks.
IIRC on the Mac newline is \n, but this is not the question.
If you are seeing the four characters "\r\n" on the screen then I assume the string coming from db is escaping the backslashes. Thus when you do the manually rewrite the (escaped) string \\r\\n is converted into \n.
The standard cell is using an embedded UILabel anyway, so rewriting your cell with a custom cell using another UILabel would not change much. ;)
I just finished fixing this very issue in my code using:
myString = [[myString
stringByReplacingOccurrencesOfString:#"\\\\n" withString:#"\n"]
stringByReplacingOccurrencesOfString:#"\\\\r" withString:#""];
Hope that helps

Emoji iCons are not displaying correctly when read from plist

I am trying to read some text from a plist file and display it to the users in alert box.
When I build the string using this code, everything works (users sees Hello with a smily icon):
NSString *hello = #"Hello \ue415";
but when I get the string from plist, using this code, uses sees "Hello \ue415":
NString *hello = (NSString *)[pageLiteratureDic objectForKey:litratureKey];
Do I have to encode string differently? Any help or pointers will be much appreciated... everyone love emojis ;)
You shouldn't literally type "\ue415" as text into the plist file. \u.... is an escape sequence in the syntax of strings and characters in the C language. The string itself does not contain backslash and "u" and whatever, it contains just 1 character, the Unicode character at the codepoint 0xe415. If you want to save that in a plist, you have to manually type that one Unicode character in there yourself, making sure to use whatever encoding that is required of a plist (maybe utf-8 or utf-16, not sure). Alternately, you can write a program that creates a plist from that string, and then copy and paste whatever is in that plist file over to your file.
In the plist, instead of "Hello \ue415" try using the smily face character explicitly as in "Hello :)". Just cut and paste the smily character over the unicode code. The reading of the plist is probably escaping the backslash and stopping the interpretation as a unicode character.

Problem with \t \n with NSString

I have a plist where I place my strings, with type string.
When I access the strings in the plist, it doesnt interpret \n and \t.
So when I display these strings it will display \tTitle
and not Title with tab indention.
But if I use
#define str1 #"\tTitle"
and just place it in header files, it works.
Please help.
Maybe something is wrong with the way you are accessing the string. Take a look here: Property list programming guide