【问题标题】:How to include newline on objective-c when reading from resource string从资源字符串读取时如何在objective-c上包含换行符
【发布时间】:2012-03-26 15:58:34
【问题描述】:

使用字符串时 NSString *Content = @"mytestline one \n My second line"; MyTextView1.text = Content; textview 正确打印出 2 行。

从资源中读取相同字符串时 NSString *Content = ReadMyStringClass MyTextView1.text = Content;

TextView 只打印出包含非转义字符“\n”内容的一行。 如何在 Textview 上创建换行符?

ReadMyStringClass 的代码

NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:@"XmlDictionary.plist"];
NSDictionary *XmlDictionary = [NSDictionary dictionaryWithContentsOfFile: finalPath];

NSString *WelcomeText = [XmlDictionary objectForKey:@"WelcomeMessage"];

【问题讨论】:

  • 我已经更新了关于您的问题的问题。

标签: objective-c string resources newline


【解决方案1】:

问题出在 plist 文件中的字符串中。它不包含换行符,而是两个字符\n。因此,您应该 1) 编辑 plist 文件,并通过按 + 插入 "real" 换行符或 2) 更改以编程方式出现\n

NSString *WelcomeText = [[XmlDictionary objectForKey:@"WelcomeMessage"] stringByReplacingOccurrencesOfString:@"\\n" withString:@"\n"];

【讨论】:

  • 好的,没认出来,字符串被转换为\\n。感谢您的建议!
猜你喜欢
  • 1970-01-01
  • 2012-04-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-22
  • 2014-01-15
  • 1970-01-01
  • 2015-06-30
相关资源
最近更新 更多