自定义从 UITextView 打开的自动 MFMailComposeViewController

2024-02-02

我有一个简单的UITextView其中有一个电子邮件链接。文本视图是可选择的并检测链接。这样,您可以单击电子邮件,它会以模式方式打开MFMailComposeViewController视图控制器。

但是,我在应用程序启动时做了一些自定义:

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

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

我的问题是,所有这些都不适用于邮件编辑器:栏是灰色的,标题是白色的,但字体是默认的 helvetica neue,按钮是默认的蓝色。而且,状态栏是黑色的,即使我的 Info.plist 说UIStatusBarStyleLightContent and View controller-based status bar appearance被设定为NO.

我知道如何定制MFMailComposeViewController当我手动调用它时,但这里它会自动弹出。我怎样才能将我的样式应用到它上面?


EDIT

定制MFMailComposeViewController的出现是一个非常糟糕的主意,很可能会让你的应用程序被苹果拒绝。仅当您不打算将应用程序提交给 Apple 时,才应使用以下解决方案。


看来我已经解决了,感谢雷·温德利希 http://www.raywenderlich.com/48001/easily-overlooked-new-features-ios-7#textViewLinks(再次..)。这是完整的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];

    […] // Initializations

    // Link detection
    [_textView.attributedText addAttribute:NSLinkAttributeName value:@"mail://contact" range:[[content string] rangeOfString:@"[email protected] /cdn-cgi/l/email-protection"]];

    _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];
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

自定义从 UITextView 打开的自动 MFMailComposeViewController 的相关文章

随机推荐