【问题标题】:Display Paragraphs in UITextView without \n在没有 \n 的 UITextView 中显示段落
【发布时间】:2011-11-23 17:10:24
【问题描述】:

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

blah

blah

@"blah\n\nblah"

谢谢!

【问题讨论】:

    标签: objective-c ios cocoa-touch line-breaks paragraph


    【解决方案1】:

    假设你有一个简单的例子:

    • 一个名为: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; 
    

    【讨论】:

      【解决方案2】:

      您可以将文本存储在 Bundle 中的 .txt 文件中并使用

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

      【讨论】:

      • 好吧……如果我只是通过将我的txt文件拖到我的xcode项目中来添加我的txt文件,该目录会是它的组吗?例如:myProject -> Group "Resources" -> myText.txt?
      • 取决于您如何创建组。如果不需要目录,可以使用 pathForResource:ofType: 代替 pathForResourcecsOfType:inDirectory:
      猜你喜欢
      • 1970-01-01
      • 2011-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-01
      • 2021-08-03
      • 2016-04-05
      • 1970-01-01
      相关资源
      最近更新 更多