Display Paragraphs in UITextView without \n

人盡茶涼 提交于 2019-12-12 21:14:02

问题


Is there a convenient way to format a rather long string with paragraphs in it (like below) to have it with \n in it so the textView will display the paragraphs as well?

blah

blah

to @"blah\n\nblah"

Thanks!


回答1:


Here's a simple example assuming you have :

  • A file named : myfile.txt
  • A string variable to hold the file's content, let's name it : content
  • A UITextView called :textView

Write whatever you want in your text file and place it wherever you want in your Xcode project (usually import it from within Xcode under "Ressources"), and then somewhere in your code (init method or action) :

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

textView.text = content; 



回答2:


You can store your text in a .txt file in your Bundle and load it with

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


来源:https://stackoverflow.com/questions/8246260/display-paragraphs-in-uitextview-without-n

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!