问题
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