Adding a line break in a UITextView

I have a UITextView that accepts an NSString with stringWithUTF8String formatting. It gets its values ​​from the database, and I want the text in the database to appear with gaps in the text. I tried using \n to do this, but it will display as text. Doing this on my application information page, as the direct text worked, but I think the reason it does not work when it is taken from the database due to formatting.

Any suggestions?

+48
iphone utf-8 nsstring whitespace uitextview
Sep 13 '10 at 0:24
source share
11 answers

If you want to add a new line to the Xcode5 / Xcode6 storyboard Attribute-Inspector (AI), you will do this:

  • Pull out a UITextView.
  • Double-click it to edit the text, place the cursor where you want to break the paragraph.
  • Press alt-Enter a couple of times to create an empty line and paragraph.

Or, CTRL + Return make a new line.

Now you can see the effect in the output / view mode in the storyboard. enter image description here

+69
Mar 08 '15 at 15:15
source share

Expand tc a bit: when you have newlines, you probably want to convert them to \n (that is, @"\\n" in Cocoa), then save them to the database and get the BACK string from the database, you probably want to convert them back to newlines. So you would use code 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"]; 
+35
Sep 25 2018-10-09T00:
source share

Just 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 displayed below in your text element or in any other place that you select

Exit:

January: 7, 21,

February: 4, 18,

March: 4th, 18th, etc. Etc.

+19
Mar 30 2018-11-11T00:
source share

I think you should try using "\ r".

I tested it on Xcode 4.2.1, iOS 4.3

Edit: this also works in iOS 3.0 and iOS 5.1

+7
Feb 27 2018-12-12T00:
source share

In Swift, just add "\ n" between the two lines

Example:

 let firstString = "Hello" let secondString = "Senorita" textview.text = firstString + "\n" + "secondString" 
+5
Oct 30 '15 at 5:04
source share

UITextView will accept newlines, but it will not work with caries returns. You will need to make sure your entry is correct.

I tried it in IB editor, and at first had some problems with it. Then I used a text editor, typed the text and pasted it into the IB editor.

This is a trick for me. Your source comes from the database, so I think the newly-made lines are new lines, but carriage returns or something else. Check the HEX line of your input.

+2
Mar 06 '13 at 13:33
source share

Set the keyboard type to (UIKeyboardType) and (UIReturnKeyTYpe) returnKeyType

if you are building code.

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

+1
Sep 24 '10 at 9:44
source share

when you receive from db. try

 NSString *content = [mytext stringByReplacingOccurrencesOfString:@"\\n" withString:@"\n"]; self.textView.text = content; 
0
01 Oct '15 at 2:28
source share

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

as an example (php):

 echo $item_detail['item_title']." \n "; 
0
Aug 02 '16 at 11:48 on
source share

Olie answer extension ... In Swift, the code for updating a row from a database is:

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

It took a while to figure it out, so I thought that I would share.

-one
Jul 08 '15 at 18:52
source share

You save the literal "\ n" in the database. Try to save a new line.

-2
Sep 13 '10 at 1:40
source share



All Articles