68

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?

l30c0d35
  • 777
  • 1
  • 8
  • 32
Mark Cicero
  • 737
  • 1
  • 6
  • 5

12 Answers12

101

If you want to add newline in Xcode5 / Xcode6 storyboard Attribute-Inspector (AI), you do it this way:

  1. Drag out a UITextView.
  2. Double-click it to edit text, place cursor where you want the paragraph break.
  3. 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.enter image description here

bneely
  • 9,083
  • 4
  • 38
  • 46
imti
  • 1,283
  • 1
  • 11
  • 12
39

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"];
sudo make install
  • 5,629
  • 3
  • 36
  • 48
Olie
  • 24,597
  • 18
  • 99
  • 131
  • Tommecpe made a good point in an edit proposal - it is stringByReplacingOccurrencesOfString - see https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/stringByReplacingOccurrencesOfString:withString: – Chris Stratton May 29 '13 at 17:19
  • I think this is smart way of cheating to get it odne – Fahim Parkar Oct 03 '13 at 06:24
26

Simply the below:

{
textView.text = @"January: 7th,21st,\n February: 4th,18th,\n March: 4th,18th,\n April: 1st, 15th, 29th, \n May: 13th, 27th, \n June: 10th,24th, \n July: 8th, 22nd, \n August: 5th, 19th, \n September: 2nd, 16th, 30th, \n October: 14th, 28th, \n November: 11th, 25th";
        NSLog (@"Recycle Dates for Abbey Way");

}

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

Shardul
  • 4,266
  • 3
  • 32
  • 50
Alex McPherson
  • 3,185
  • 3
  • 30
  • 41
13

In Swift, just add "\n" between two strings

Example:

let firstString = "Hello"

let secondString = "Senorita"

textview.text = firstString + "\n" + "secondString"
Hardik Thakkar
  • 15,269
  • 2
  • 94
  • 81
Ravindra Kishan
  • 183
  • 1
  • 6
7

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

bneely
  • 9,083
  • 4
  • 38
  • 46
hellangle
  • 425
  • 3
  • 7
3

when you get from db. try

NSString *content = [mytext stringByReplacingOccurrencesOfString:@"\\n" withString:@"\n"];
self.textView.text = content;
Tuan Pham
  • 621
  • 9
  • 9
2

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.

Leander
  • 612
  • 7
  • 8
1

Set the (UIKeyboardType)keyboardType and (UIReturnKeyTYpe) returnKeyType

if you are building in code.

If you are using IB then just go to the first inspector panel and set the TextInputTraits

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
1

if you are getting the text from web, make sure you put a space before and after the /n that solved my problem

as example (php):

echo $item_detail['item_title']."  \n ";
Omar N Shamali
  • 503
  • 5
  • 11
0

Expanding on Olie's answer... In Swift the code for updating the string from the database is:

let dbString = // get the string from the db.
myTextField.text = dbString.stringByReplacingOccurrencesOfString("\\\\n", withString: "\n")

Took me a while to figure out, so thought I would share.

bwait
  • 29
  • 4
0

Append a character("\n") in the string

var logs = "this is first Line" self.logs.append(Character("\n")) self.textView.text = self.logs

Deepak Singh
  • 396
  • 1
  • 12
-2

You're saving a literal "\n" in the database. Try saving a newline.

tc.
  • 33,468
  • 5
  • 78
  • 96