【问题标题】:changing the MFMailComposeViewController navigationBar title font更改 MFMailComposeViewController 导航栏标题字体
【发布时间】:2012-06-23 23:04:17
【问题描述】:

我有一个MFMailComposeViewController,当我设置了主题时,它也将其设置为主题。 问题是如何自定义标题的字体和颜色?

MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
[picker setSubject:@"Feedback for MyApp"];

【问题讨论】:

    标签: ios objective-c iphone ipad mfmailcomposeviewcontroller


    【解决方案1】:

    我已经从 app-delegate 为所有视图控制器的所有导航栏更改了我的应用程序的字体。如果您不介意更改所有导航栏标题字体,请在 applicationDidFinishLaunching:withOptions:

    中添加类似以下内容
        if ([[UINavigationBar class] respondsToSelector:@selector(appearance)])
            // set the navigation bar font to "courier-new bold 14"
            [[UINavigationBar appearance] setTitleTextAttributes:
             [NSDictionary dictionaryWithObjectsAndKeys:[UIColor lightGrayColor], UITextAttributeTextShadowColor,
                                                        [NSValue valueWithUIOffset:UIOffsetMake(1, 0)], UITextAttributeTextShadowOffset,
                                                        [UIFont fontWithName:@"CourierNewPS-BoldMT" size:14.0], UITextAttributeFont, 
                                                        nil]];
    

    更新: 如果您希望它特定于这种情况,您可能可以使用 appearanceWhenContainedIn: 使用 Alexander Akers 在下面的评论中建议的参数,因为 UIViewController 继承自 UIAppearanceContainer。我知道appearanceWhenContainedIn:在其他情况下有效,例如UINavigationBar中包含的UIBarButtonItems,因此在这种情况下它应该类似地工作。

    【讨论】:

    • [UINavigationBar appearanceWhenContainedIn: [MFMailComposeViewController class], nil] ?
    • 该代码仅在包含在邮件撰写视图控制器中时才会更改导航栏的外观。
    • @AlexsanderAkers,更新了答案以纳入您的建议。我有使用 AppearanceWhenContainedIn: 用于 UINavigationBar 中的 UIBarButtonItem 的经验;我没有在这种情况下使用它,但它确实可以工作。
    【解决方案2】:

    我的猜测是由于跨进程限制而无法完成。我可以更改颜色、大小和字体(如果它是内置系统字体),但尝试将其更改为我的应用程序中包含的任何字体都会失败。

    【讨论】:

      【解决方案3】:

      由于MFMailComposeViewControllerUINavigationController的一个实例,你可以设置它的topViewController的titleView如下:

      UIViewController *controller = [mailController topViewController];
      
      UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 170.0f, 44.0f)];
      titleView.backgroundColor = [UIColor clearColor];
      
      UILabel *label = [[UILabel alloc] initWithFrame:titleView.bounds];
      label.textColor = [UIColor whiteColor];
      label.backgroundColor = [UIColor clearColor];
      label.text = @"Custom Font";
      label.font = [UIFont italicSystemFontOfSize:27.0f];
      label.textAlignment = UITextAlignmentCenter;
      
      [titleView addSubview:label];
      [label release];
      
      controller.navigationItem.titleView = titleView;
      [titleView release];
      

      【讨论】:

      • 由于某种原因这不起作用,我复制粘贴了您的代码并用我的替换了 mailController
      • @xonegirlz,你提到过,你试过这段代码。我记得这种更改控制栏特征的方式有问题,这就是为什么我开始依赖上面回答中描述的 UIAppearance 的东西。你也试过了吗?有更新吗?
      猜你喜欢
      • 2017-01-19
      • 2012-01-14
      • 2014-07-21
      • 1970-01-01
      • 2023-03-09
      • 2020-04-01
      • 2018-02-13
      • 2015-12-24
      • 1970-01-01
      相关资源
      最近更新 更多