【发布时间】:2016-03-10 22:03:18
【问题描述】:
从我的 Swift iOS 应用重定向时,我的 SMS 消息的默认正文中是否可以有换行符?
【问题讨论】:
-
您是否尝试将
\n添加到您的字符串中? -
如果你使用UILabel来显示,你是否将它的行数设置为0(无限)?
标签: ios swift messages messageui
从我的 Swift iOS 应用重定向时,我的 SMS 消息的默认正文中是否可以有换行符?
【问题讨论】:
\n 添加到您的字符串中?
标签: ios swift messages messageui
绝对可以在 SMS 消息中创建换行符。正如@EmilioPalaez 所指出的,它是通过使用转义\n 来实现的。下面的代码演示了如何做到这一点。
// Create Message Controller //
let messageComposeVC = MFMessageComposeViewController()
// Set Its Delegate //
messageComposeVC.messageComposeDelegate = self
// Set Recipient(s) //
messageComposeVC.recipients = ["555-555-5555"]
// Create Body Text (Note the use of "\n") //
messageComposeVC.body = "This is your message. \nThis is your message on a new line."
// Present Message Controller //
presentViewController(messageComposeVC, animated: true, completion: nil)
更新
您也可以删除转义换行符 (\n) 并简单地使用块文本:
// Create Message Controller //
let messageComposeVC = MFMessageComposeViewController()
// Set Its Delegate //
messageComposeVC.messageComposeDelegate = self
// Set Recipient(s) //
messageComposeVC.recipients = ["555-555-5555"]
// Create Body Text (Note the use of an opening and closing """) //
messageComposeVC.body = """
This is your message.
This is your message on a new line.
"""
// Present Message Controller //
presentViewController(messageComposeVC, animated: true, completion: nil)
通过使用""" 表示一个文本块,我们可以简单地以视觉方式格式化文本。
使用这种创建字符串的方法,我们可以在不使用某些(不是全部)特殊转义字符的情况下将项目放在新行、缩进等。我们可以在这里简单地删除转义的换行符 (\n),然后将预期的文本放在实际的换行符上。
【讨论】: