在没有\ n的UITextView中显示段落

有一个方便的方法来格式化一个相当长的string与段落(如下),使其与\n在它所以textView将显示段落以及?

 blah blah 

to @"blah\n\nblah"

谢谢!

假设您有以下一个简单的例子:

  • 一个名为:myfile.txt的文件
  • 保存文件内容的stringvariables,我们将其命名为:content
  • 一个叫做textView的UITextView

在你的文本文件中写下任何你想要的东西,然后把它放在Xcode项目中的任何地方(通常从Xcode的“Ressources”下面导入),然后在你的代码的某个地方(init方法或者动作):

 NSString *path = [[NSBundle mainBundle] pathForResource:@"myfile" ofType:@"txt"]; NSString *content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:NULL]; textView.text = content; 

你可以将你的文本保存在Bundle的.txt文件中并加载

 [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"youTextFile" ofType:@"txt" inDirectory:@"yourRessourceDirectory"] encoding:NSUTF8StringEncoding error:nil];