Objective c 在UITextView中显示段落而不显示\n

Objective c 在UITextView中显示段落而不显示\n,objective-c,ios,cocoa-touch,line-breaks,paragraph,Objective C,Ios,Cocoa Touch,Line Breaks,Paragraph,是否有一种方便的方法来格式化包含段落的较长字符串(如下所示),使其包含\n,以便textView也显示段落 blah blah 到@“blah\n\nblah” 谢谢 您可以将文本存储在捆绑包中的.txt文件中,并使用 [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"youTextFile" ofType:@"txt" inDirectory:@"yourRessourceDirector

是否有一种方便的方法来格式化包含段落的较长字符串(如下所示),使其包含
\n
,以便textView也显示段落

blah

blah
@“blah\n\nblah”


谢谢

您可以将文本存储在捆绑包中的.txt文件中,并使用

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

下面是一个简单的示例,假设您有:

  • 一个名为:myfile.txt的文件
  • 用于保存文件内容的字符串变量,我们将其命名为:content
  • 一个名为:textView的UITextView
在文本文件中编写您想要的内容,并将其放置在Xcode项目中的任何位置(通常从Xcode中的“resources”下导入),然后将其放置在代码中的某个位置(init方法或操作):


好的……如果我只是通过将txt文件拖动到xcode中的xcode项目中来添加它,那么目录就是它的组吗?例如:myProject->Group“Resources”->myText.txt?取决于您创建组的方式。如果不需要目录,可以使用pathForResource:ofType:而不是PathForResourceSoftType:inDirectory:
NSString *path = [[NSBundle mainBundle] pathForResource:@"myfile" 
                                                 ofType:@"txt"];
NSString *content = [NSString stringWithContentsOfFile:path 
                                              encoding:NSUTF8StringEncoding 
                                                 error:NULL];

textView.text = content;