MonkeyBlue
MonkeyBlue

Reputation: 2234

iOS UITextView ignoring newline char from JSON Response

I need to display some data from a JSON Webservice in my iOS App inside a UITextView which is multiline however when I use the String on the textview the new lines do not work.

Here is the code I am using:

// Data Value from JSON = testing the data value \r\n Another \n liine maybe?
NSString *responseDataString = [dictRequestorDetails objectForKey:@"data"];
txtWorkflowDetails.text = responseDataString;

When I execute this code the UITextField shows

testing the data value \r\n Another \n liine maybe?

However If I type in the string like

// Data Value from JSON = testing the data value \r\n Another \n liine maybe?
NSString *responseDataString = [dictRequestorDetails objectForKey:@"data"];
txtWorkflowDetails.text = @"testing the data value \r\n Another \n liine maybe?";

The line breaks then work in the TextView

Upvotes: 2

Views: 4798

Answers (4)

Serhii Didanov
Serhii Didanov

Reputation: 2348

Swift 4:

let stringForUITextView = originJsonString.replacingOccurrences(of: "\\n", with: "\n")

Upvotes: 1

MonkeyBlue
MonkeyBlue

Reputation: 2234

Just followed this post: Adding a line break to a UITextView

I updated my JSON feed to output the new lines like

testing the data value \nAnother \nmline maybe?

And then in my code ran;

responseDataString = [responseDataString stringByReplacingOccurrencesOfString:@"\\n" withString:@"\n"];

Upvotes: 3

ApplinguaRob
ApplinguaRob

Reputation: 304

Is txtWorkflowDetails set to allow Line Breaks (e.g. Word Wrap)?

Upvotes: 0

Sumit Mundra
Sumit Mundra

Reputation: 3911

You can process the string first and replace all occurrences of \n with \n

NSString *newString = [responseDataString stringByReplacingOccurrencesOfString:@"\n" withString:@"\\n"];

Upvotes: 0

Related Questions