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

发布于 2024-12-17 14:08:11 字数 174 浏览 3 评论 0原文

有没有一种方便的方法来格式化一个相当长的字符串,其中包含段落(如下所示),使其包含 \n ,这样 textView 也会显示段落?

blah

blah

发送至@"blah\n\nblah"

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

嗼ふ静 2024-12-24 14:08:11

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

  • 一个名为: myfile.txt 的文件
  • 一个用于保存文件内容的字符串变量,我们将其命名为: 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; 

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; 
渡你暖光 2024-12-24 14:08:11

您可以将文本存储在 Bundle 中的 .txt 文件中,并使用以下命令加载它

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

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];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文