I have a UITextView
that takes an NSString
with formatting stringWithUTF8String
. It is getting its values from a database and I want the text in the database to be rendered with breaks within the text. I tried using \n
to do this but it gets rendered as text. Doing this in my information page of the app as straight text worked but I think the reason its not working when taking it from the database is because of the formatting.
Any suggestions?
A UITextView will take the newlines, but it will not work with cariage returns. You will have to make sure your input is correct.
I tried it in the IB editor, and had some troubles with it initially. Then I used a text editor, typed my text and then pasted it into the IB editor.
That did the trick, for me. Your source comes from a database, so I think probaly the newlines are actually newlines, but perhaphs carriage return's or other. Check the HEX string of your input.
In Swift, just add "\n" between two strings
Example:
I think, you should try using "\r".
I have tested it on Xcode 4.2.1, iOS 4.3
Edit: This also works in iOS 3.0 and iOS 5.1
Expanding tc's answer a bit: when you have newlines in the string, you probably want to convert them to
\n
(that is,@"\\n"
in Cocoa) then save to the database and, on getting the string BACK from the database, you probably want to convert those back to newlines. So you'd use code something like this:Simply the below:
will be out putted as below in your textView or whatever you choose
Output :
January: 7th, 21st,
February: 4th, 18th,
March: 4th, 18th etc etc
You're saving a literal "\n" in the database. Try saving a newline.