自定义从UITextView打开的自动MFMailComposeViewController [英] Customizing automatic MFMailComposeViewController opened from UITextView

查看:78
本文介绍了自定义从UITextView打开的自动MFMailComposeViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的 UITextView ,里面有一个电子邮件链接。 textview是可选择的并检测链接。通过这种方式,您可以单击电子邮件,它以模式方式打开 MFMailComposeViewController 视图控制器。

I have a simple UITextView with an email link in it. The textview is selectable and detects links. This way, you can click on the email and it opens modally an MFMailComposeViewController view controller.

但是,我这样做在应用程序启动时进行一些自定义:

But, I do some customization at the launch of the app :

[[UINavigationBar appearance] setBarTintColor: myGreyColor];
[[UINavigationBar appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor], NSFontAttributeName: myFont}];
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];

这样,所有导航栏都是灰色的,带有白色文字和白色按钮,标题有一个自定义字体。

This way, all navigation bars are grey, with white text and white buttons, and the title has a custom font.

我的问题是所有这些都没有应用于邮件编辑器:栏是灰色的,标题是白色的,但字体是默认的helvetica neue并且按钮是默认的蓝色。而且,状态栏是黑色的,即使我的Info.plist显示 UIStatusBarStyleLightContent 查看基于控制器的状态栏外观设置为

My problem is that all these are not applied to the mail composer : the bar is grey and the title is white, but the font is the default helvetica neue and the buttons are the default blue. And, the status bar is black, even though my Info.plist says UIStatusBarStyleLightContent and View controller-based status bar appearance is set to NO.

我知道如何自定义 MFMailComposeViewController 当我手动调用它时,它会自动弹出。如何应用我的样式?

I know how to customize MFMailComposeViewController when I call it manually, but here it pops up automatically. How can I have my styles applied to it ?

推荐答案

编辑

自定义 MFMailComposeViewController 的外观是一个非常糟糕的主意,很可能会让Apple拒绝您的应用。只有在您不打算将应用程序提交给Apple时才能使用以下解决方案。

Customizing the MFMailComposeViewController's appearance is a really bad idea and will most likely get your app rejected by Apple. The following solution should only be used if you don't intend to submit your app to Apple.

看起来我解决了感谢 Ray Wenderlich (再次。 )。以下是完整代码:

Looks like I solved it, thanks to Ray Wenderlich (again..). Here is the full code :

- (void)viewDidLoad
{
    [super viewDidLoad];

    […] // Initializations

    // Link detection
    [_textView.attributedText addAttribute:NSLinkAttributeName value:@"mail://contact" range:[[content string] rangeOfString:@"contact@mymail.com"]];

    _textView.delegate = self;

}

// Handle the link tap yourself
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange {

    if ([[URL scheme] isEqualToString:@"mail"]) {

        MFMailComposeViewController *mailVC = [[MFMailComposeViewController alloc] init];
        [mailVC setToRecipients:@[@"contact@ mymail.com"]];
        [mailVC setSubject:@"About QuickReminder for iOS"];
        mailVC.mailComposeDelegate = self;

        // Re-set the styling
        [mailVC.navigationBar setBarTintColor:myGreyColor];
        [mailVC.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor], NSFontAttributeName: myFont}];
        [mailVC.navigationBar setTintColor:[UIColor whiteColor]];

        [self presentViewController:mailVC animated:YES completion:^{
            [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
        }];

        return NO;
    }
    return YES;
}

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
    // Notifies users about errors associated with the interface
    switch (result)
    {
        case MFMailComposeResultCancelled:
            NSLog(@"Result: canceled");
            break;
        case MFMailComposeResultSaved:
            NSLog(@"Result: saved");
            break;
        case MFMailComposeResultSent:
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Result" message:@"Mail Sent Successfully" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alert show];
        }
            break;
        case MFMailComposeResultFailed:
            NSLog(@"Result: failed");
            break;
        default:
            NSLog(@"Result: not sent");
            break;
    }

    [controller dismissViewControllerAnimated:YES completion:nil];
}

这篇关于自定义从UITextView打开的自动MFMailComposeViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆