在UITextView中添加换行符

Mar*_*ero 63 whitespace utf-8 nsstring uitextview

我有一个UITextView接受一个NSString与格式stringWithUTF8String.它从数据库获取它的值,我希望数据库中的文本在文本中呈现中断.我尝试使用\n这样做,但它被渲染为文本.在我的应用程序的信息页面中以直接文本的方式执行此操作,但我认为从数据库中取出它时不起作用的原因是因为格式化.

有什么建议?

imt*_*mti 90

如果要在Xcode5/Xcode6故事板属性检查器(AI)中添加换行符,可以这样做:

  1. 拖出UITextView.
  2. 双击它以编辑文本,将光标放在您想要段落的位置.
  3. 点击alt-输入几次以创建一个空白行和段落.

或者,CTRL + Return创建一个新行.

您现在可以在故事板中看到效果到输出/视图模式.在此输入图像描述


Oli*_*lie 37

稍微扩展tc的答案:当你在字符串中有换行符时,你可能想要将它们转换为\n(即@"\\n"在Cocoa中)然后保存到数据库中,并且在从数据库获取字符串BACK时,你可能想要转换它们回到换行符.所以你要使用这样的代码:

NSString *saveString = [myTextField.text stringByReplacingOccurrencesOfString: @"\n" withString: @"\\n"];
// save it to the db
// ... later ...
NSString *dbString = // get the string from the db.
myTextField.text = [dbString stringByReplacingOccurrencesOfString: @"\\n" withString: @"\n"];
Run Code Online (Sandbox Code Playgroud)


Ale*_*son 22

简单如下:

{
textView.text = @"January: 7th,21st,\n February: 4th,18th,\n March: 4th,18th,\n April: 1st, 15th, 29th, \n May: 13th, 27th, \n June: 10th,24th, \n July: 8th, 22nd, \n August: 5th, 19th, \n September: 2nd, 16th, 30th, \n October: 14th, 28th, \n November: 11th, 25th";
        NSLog (@"Recycle Dates for Abbey Way");

}
Run Code Online (Sandbox Code Playgroud)

将在您的textView或您选择的任何内容中如下所示

输出:

1月:7月21日,
2月:4
月18日, 3月:4日,18日等


小智 10

在Swift中,只需在两个字符串之间添加"\n"即可

例:

let firstString = "Hello"

let secondString = "Senorita"

textview.text = firstString + "\n" + "secondString"
Run Code Online (Sandbox Code Playgroud)


小智 7

我想,你应该尝试使用"\ r".

我在Xcode 4.2.1,iOS 4.3上测试过它

编辑:这也适用于iOS 3.0和iOS 5.1


tc.*_*tc. -2

您正在数据库中保存文字“\n”。尝试保存换行符。