UITextView: How to set '\n' from an XML-loaded string?

I need to indicated a 'new line' in a string in an XML file I'm loading.

If I hard code the string:

myTextView.text = [NSString stringWithString:@"Line1 \nLine2 \nLine3"];

It displays:


(as expected)

But if I put the exact string in my XML file:

<someNode string="Line1 \nLine2 \nLine3" />

And then load it into the UITableView it displays:

Line1 \nLine2 \nLine3

I've read somewhere that the '\n' character is processed at compile time, is this true? This would explain the behavior I'm seeing.

If this is the case, how else can I define a new line in my XML-loaded string and still have it properly processed in the UITextView? Is there another way to do this?



You are right about \n being parsed at compile time.

NSString *b = [a stringByReplacingOccurrencesOfString:@"\\n" withString:@"\n"];

should do it!

BTW you may need to do similar replacements for \\, \", \t and the likes.

Yes, the \n to newline conversion is done by your compiler.

In XML you can't really have a newline in an attribute value due to Attribute-Value Normalization. Any newline would just get converted to a space by a standards conformant parser. You can, however, have newlines in XML elements, so if you can change your XML to use elements rather than attributes for holding this text then you could just have literal newlines in your XML.

If you must use attributes, then you'll have to build your own representation of newlines on top of XML (as mvds suggests).

Need Your Help

getElementById in Safari

safari applescript

I want do download my epaper using AppleScript and Safari. Here's my code:

Unable to start T-SQL Debugging. Could not connect to computer ("*****")

sql sql-server debugging

When I try to debug an Stored Procedure using Microsoft SQL Server Management Studio, I encounter this error message: Unable to start T-SQL Debugging. Could not connect to computer ("*"). The dubug...