Adding a line break to a UITextView
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?
If you want to add newline in Xcode5 / Xcode6 storyboard Attribute-Inspector (AI), you do it this way:
- Drag out a UITextView.
- Double-click it to edit text, place cursor where you want the paragraph break.
- Hit alt-Enter a couple of times to create a blank line & paragraph.
Or, CTRL + Return make a new line.
You can now see the effect into output / View-mode in storyboard.
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:
NSString *saveString = [myTextField.text stringByReplacingOccurrencesOfString: @"\n" withString: @"\\n"];
// save it to the db
// ... later ...
NSString *dbString = // get the string from the db.
myTextField.text = [dbString stringByReplacingOccurrencesOfString: @"\\n" withString: @"\n"];