我是一名新手和业余程序员。我正在尝试为 iOS 创建一个应用程序来填充我的 UITextFields 中的文本。我有 3 个 actionField、impactField 和 result 字段。如果可能的话,我希望每个人都有自己的台词。
我看了类似的答案,但对我没有任何帮助。有没有人有任何教程或建议?
- (IBAction)backgroundTap: (id)sender {
[self.actionField resignFirstResponder];
[self.impactField resignFirstResponder];
[self.resultField resignFirstResponder];
}
- (IBAction)sendBullet:(id)sender {
NSString *emailTitle = @"Email";
NSString *messageBody = @"Title";
NSArray *toRecipients = [NSArray arrayWithObject:@"email"];
MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
[mc setSubject:emailTitle];
[mc setMessageBody:messageBody isHTML:NO];
[mc setToRecipients:toRecipients];
[self presentViewController:mc animated:YES completion:NULL];
最佳答案
欣顿
在继续之前,您应该看一下 HTML 语法,因为它已经存在了几十年,所以那里有成千上万的教程。很容易拿起。你可以开始HERE
注意 最好在设备上测试 HTML 电子邮件,因为模拟器对于 MFMailComposeViewController
可能有点不稳定。
其次,从 TextView 或其他形式的字符串中抓取文本相当容易,同样,那里有数以千计的教程。专门针对 Obj C,您可以按如下方式执行:
1) 为你的文本框创建一个 outlet
@interface YourViewController: UIViewController
@property (nonatomic, retain) IBOutlet UITextField *actionField
@end
2) 然后你可以像这样在你的实现文件 (.m) 的任何地方引用它:
NSString *emailMessageBody = self.actionField.text;
为电子邮件显示 HTML 格式的文本
只要您花大约 15 分钟学习 HTML 语法,创建 HTML 格式的电子邮件就相对容易了。
如果您想在不同的行中插入您的 UITextFields,您可以实现相当于换行符(或按回车键)的 HTML:<br>
因此,要在 MFMailComposeViewController
中传递一个示例字符串看起来像这样:
NSString *emailMessageBody = [NSString stringWithFormat:@"The retrieved text from actionField is : <br><strong><font color=\"red\">'%@'</font><br></strong><br> The retrieved text from impactField is : <br>%@<br> The retrieved text from resultField is : <br>%@<br>", self.actionField.text, self.impactField.text, self.resultField.text];
只要您了解 HTML 语法,您的选择是无限的。 那么这是怎么回事?
-
<br>
创建换行符 -
<strong>
创建粗体样式文本并结束您放置的格式</strong>
在你想要大胆的最后一句话之后 -
<font>
不言自明;你也必须结束这种风格格式。
您在 MFMailComposeViewController
中调用它通常,但是您必须明确地将电子邮件设置为 HTML,以便可以从格式中删除所有 HTML 标记,因此它不会将其显示为纯文本。
[mc setMessageBody:emailMessageBody isHTML:YES];
其他常用的 HTML 标签是:
-
<style>
格式化 -
<p>
对于段落 -
<h1>
标题 -
<mark>
突出显示 -
<small>
对于小文本 -
<i>
或<em>
用于斜体或强调
更多HTML标签请引用上面的链接
SWIFT 语法
var emailMessageBody = NSString(format:"The retrieved text from actionField is : <br><strong><font color=\"red\">'%@'</font><br></strong><br> The retrieved text from impactField is : <br>%@<br> The retrieved text from resultField is : <br>%@<br>", actionField.text, impactField.text, resultField.text) as String
mailComposerVC.setMessageBody(emailMessageBody, isHTML: true)
关于ios - 从 UITextField 文本创建 HTML 电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27880424/