UITableViewCell newline characters - iphone

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

Related

Save UITextView String With \n Instead of Line Breaks (Swift)

I have a UITextView in my Swift app in which users can input text. They can input text with as many line breaks as they like but I need to save the string with the newline command (\n). How would I do this?
For example, my user inputs
Line 1
Line 2
Line 3
in the UITextView. If I was to retrieve the string...
let string = textview.text!
this would return
"Line 1
Line 2
Line 3"
when I would like for it to return
"Line1\nLine2\nLine3"
How would I go about doing this? Can I use a form of replacingOccurrences(of:with:)? I feel like I'm missing a fairly obvious solution...
Eureka! After WAY too much research and learning all about String escapes, I found a very simple solution. I'm quite surprised that this isn't an answer out there already (as far as I can tell haha) so hopefully, this helps someone!
It's actually quite simple and this will work of any String you could be using.
textView.text!.replacingOccurrences(of: "\n", with: "\\n")
Explanation:
Ok so as you can tell, it's quite simple. We want to replace the newline command \n with the string "\n". The problem is that if we replace \n with \n, it's just going to transfer over to a newline, not a string. This is why escapes are so important. As you can see, I am replacing \n with \\n. By adding an extra \ we escape the command \n entirely which turns it into a string.
I hope this helps someone! Have a great day!
Have you tried replacing \r with \n? or even \r\n with \n?
I hope I am not making an obvious assumption you considered, but maybe this may come in handy:
Is a new line = \n OR \r\n?

Remove characters from string iOS

I have an XML which I am using to parse news. News have a description. I'm using NSString to show that description in UILabel.
But, the description comes like this:
Bad news for Windows’ the researches show that Windows’ for years....
And it is being showed with those unwanted characters in UILabel. The numbers are changing in every string. They are not the sames.
I want to remove the characters that begins with &# and the numbers that follow. How can I do that? Which string encoding format should I use?
Thanks a lot.
EDIT: I don't have just one string. If I remove &#8217 from this one, there might be &#7610 in another one. It won't be removed.
I can remove &# characters and numbers too. But when I do that, In a string like that "In 1980, Jobs told us to&#2540 do something" the output will be "In , Jobs told us to do something" 1980 will be gone too, but I don't want that. That's a problem either.
These are ASCAII symbols so you need to use utf-8 string.check this link
so use this line of code
NSString *resultString = [NSString stringWithUTF8String:myAsciiString];

Adding a newline to a UITextView

I've been searching for an answer to this question on google and some other forums, but I haven't found the answer yet. It seems like a simple enough question: how do I manually insert a newline into UITextView's text field?
If it matters, I'm reading my string from an SQLite database. That means that I'm translating a const char (I think - at least I've been casting it as such) into an NSString before I use it in UITextView's -setText. I've tried inserting '\n' into the SQLite database, but after everything is translated and assigned it comes back as '\n' on the iPhone screen. Is there another character I can use?
Escape sequences in strings are interpreted at compile time and replaced with their actual byte value - therefore they have no meaning outside of the compiler.
Passing the string as it came out of your database to [UITextView setText:] is all you need to do. Your database should be holding line breaks as-is (0x0A).
Andrew

UILabel displaying Unicode Characters

I have an NSString that then sets a UILabel. This contains unicode such as...
E = MC Hammer\U00ac\U2264
and complete ones such as
\U2013\U00ee\U2013\U00e6\U2013\U2202\U2013\U220f\U2013\U03c0 \U2013\U00ee\U2013\U220f\U2013\U03c0\U2013\U00aa\U2013\U221e\U2014\U00c5
These are not displaying correctly, is there anything I need to do to parse these at all?
"U" needs to be a lowercase "u"
What code are you using to set the NSString? It's possible that the string is not being initialized properly, and that's having a negative impact on the UILabel.

NSString #"\" adding backslash character objective-c

Does anyone know of an easy way to add a single backslash (\) to a NSString in Objective-C? I am trying to have a NSString *temp = #"\/Date(100034234)\/";
I am able to get a double backslash or no backslash, but unable to get a single backslash. Any help would be appreciated. Thanks
The string #"\\" is a single backslash, #"\\\\" is a double backslash
The strings and NSLog are working fine for me (iPhone SDK 3.1.2 and Xcode 3.2.1):
NSLog(#"\\"); // output is one backslash
NSLog(#"\\\\"); // output is two backslashes
NSLog(#"\\/Date(100034234)\\/"); // output is \/Date(100034234)\/
See this answer.
This is a bug in NSLog. I found a mailing list archive with a message dated in 2002 of someone that filed a bug for this here. The person also said this:
Nothing has been done as far as I can tell. I don't understand how
they've done it, but the escaping does work for the string, just not for
NSLog.
So I guess you will have to come up with your own implementation of a log message if you really want backslashes.
This code does give the requested output:
NSString *temp = #"\\/Date(100034234)\\/";
NSLog(#"%#",temp);
However I had an issue with my JSON toolkit (SBJSON) that replaced all occurrances of "\\" with "\\\\", which did cause issues as described in the other answers and the comments.
The result was a string looking like:
#"\\\\/Date(100034234)\\\\/"
See my answer here
The solution was using:
temp = [temp stringByReplacingOccurancesOfString:#"\\\\" withString:#"\\"];
Where you want a \ add or remove. just \\ on that place.