UITextView 文本换行

转载自:http://blog.csdn.net/pclass/article/details/6726964

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

If I hard code the string:

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


It displays: Line1
Line2
Line3

(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


原因是从XML中读取出来的"\n",系统认为是字符串,会默认转换为"\\n",所以当显示的时候就是字符串了,要想显示换行,需要自己手动将"\\n"转换为"\n",这样才能换行.

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

你可能感兴趣的:(UITextView 文本换行)