1

For the past few hours I have been trying to get rid of all the visible line breaks that my UITextView is showing. So I thought it would be as simple as running this code:

NSString *trimmedString = [textView.text stringByReplacingOccurrencesOfString:@"\n" withString:@""];
[textView setText:trimmedString];

However, it seems like nothing changes after executing that. The textView text is in fact being set to trimmedString but the line breaks are still visible like shown in the picture below.

enter image description here

How would I properly fix this so I can have the text shown in the image (as an example), be without line breaks?

SimplyKiwi
  • 12,376
  • 22
  • 105
  • 191

2 Answers2

0

I just put your example code in the viewDidLoad of my UIViewController and totally saw the expected result. Here is one screenshot without your code snippet: Showing the default state

And here is a screenshot with the code snippet: Showing modified state

You can see that the line breaks were replaced. Did you connected your UITextView Outlet correctly ?

dehlen
  • 7,325
  • 4
  • 43
  • 71
  • Yes everything is connected, both the delegate and IBOutlet to the textView so I am not sure why it isn't working as expected. – SimplyKiwi Apr 18 '15 at 19:03
  • I actually ended up finding slightly different code that fixed it. I posted it as an answer. – SimplyKiwi Apr 18 '15 at 19:13
  • Are you sure the line breaks in your text are properly formatted ? Are you sure the line breaks get detected ? Maybe you should try this and have a look if the line breaks get detected: http://stackoverflow.com/questions/9252404/detect-linebreak-in-uitextview – dehlen Apr 18 '15 at 19:14
  • 1
    Ah I see it looks like my last guess was right. The line breaks are formatted as "windows line breaks" \r\n and not Unix like \n. – dehlen Apr 18 '15 at 19:14
0

I fixed it with this code:

NSString *trimmedString = [textView.text stringByReplacingOccurrencesOfString:@"\r\n" withString:@" "];
[textView setText:trimmedString];
SimplyKiwi
  • 12,376
  • 22
  • 105
  • 191