【问题标题】:titleTextAttributes UIAppearance font in iOS 7iOS 7 中的 titleTextAttributes UIAppearance 字体
【发布时间】:2013-09-19 15:09:44
【问题描述】:

我正在使用 UIAppearance 将字体应用于 UINavigationBar 和 UIBarButtonItem,但我遇到了问题。我运行了这段代码:

[[UIBarButtonItem appearanceWhenContainedIn:[UIToolbar class], nil] 
setTitleTextAttributes:
@{NSFontAttributeName : [UIFont fontWithName:@"My_Font" size:17.0]} 
forState:UIControlStateNormal];

NSLog(@"%@", [[UIBarButtonItem appearanceWhenContainedIn:
[UIToolbar class], nil] titleTextAttributesForState:UIControlStateNormal]);

在 iOS 7 上登录的结果是:

(null)

iOS 6 中的结果是:

{
    NSFont = "<UICFFont: 0x1d897a80> font-family: \"My_Font\"; font-weight: normal; font-style: normal; font-size: 17px";
}

我在 iOS 7 文档中找不到任何表明这不应该工作的内容,还有其他人遇到过这个问题吗?

编辑 1

我实际上已经让这个与[UINavigationBar appearance] 一起工作,问题是我将点大小设置为 0,以便将字体设置为NSString UIKit Additions Reference 中描述的默认导航栏/barButtonItem 大小但是这个显然不再适用于 iOS 7。相反,将磅值设置为 0 将返回系统字体。

我仍然无法将titleTextAttributes 设置为

[UIBarButtonItem appearanceWhenContaintedIn:[UIToolbar class], nil]]

【问题讨论】:

  • 你找到解决办法了吗?
  • 不,从 iOS 7.1 开始,[UIBarButtonItem appearance] 仍然存在问题。将字典键更改为 NS 前缀或将调用移动到 -viewDidLoad 都没有解决问题。 ([UINavigationBar appearance] 只要字体的磅值大于 0 就可以工作。)

标签: ios uikit uibarbuttonitem ios7 uiappearance


【解决方案1】:
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont 
    fontWithName:@"YOURFONT" size:14], NSFontAttributeName, 
    [UIColor whiteColor], NSForegroundColorAttributeName, nil];

[[UINavigationBar appearance] setTitleTextAttributes:attributes];

关键是使用NSFontAttributeName等。我假设他们正在转向使用 NS 品种来实现 64 位兼容性。上面的代码在我的 iOS7 设备上运行。

【讨论】:

  • 嗯,有道理,但是 UIBarButtonItem 呢。当我尝试将titleTextAttributes 设置为[UIBarButtonItem appearance] 时,它似乎被忽略了。您是否能够让外观代理与 UIBarButtonItem 一起使用?也感谢您的帮助。
  • iOS7导航栏后退按钮默认字体大小是多少
  • 这为我节省了很多时间!谢谢!默认字体的名称是什么?我想匹配默认字体,但使用蓝色来匹配 iOS7 中的蓝色 UI 元素。
【解决方案2】:

按照@Alex Zavatone 的回答 - 只需一行代码就可以很好地完成:

self.navBar.titleTextAttributes = @{
    NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue-Light" size:20.0], 
    NSForegroundColorAttributeName: [UIColor redColor]
};

【讨论】:

  • 不错。很高兴能帮上忙。
  • 这对我有用,而不是使用 [UINavigationBar appearance] 代理。
【解决方案3】:

当我将它移到我的视图控制器中的 viewDidLoad 方法时,这在 iOS 7 上运行良好。

[[UIBarButtonItem appearance] setTitleTextAttributes:attributes];

当我尝试在 AppDelegate 中执行此操作时,它不起作用,尽管[[UINavigationBar appearance] setTitleTextAttributes:setTitleTextAttributes:attributes]; 在 AppDelegate 中工作正常。它也应该在您创建和分配栏按钮之前进行。

如果您在 Storyboard 上创建条形按钮,它也适用于 initWithCoder 方法。

更新: 如果您将部署目标更改为 7.0,那么 Xcode 将向您提供警告,例如 UITextAttributeTextColor 已被弃用,您应该使用 NSForegroundColorAttributeName 或使用 NSFontAttributeName 代替 UITextAttributeFont。更改属性常量后,您可以从 AppDelegate 调用 setTitleTextAttributes:。

【讨论】:

  • iOS7导航栏后退按钮的默认字体是什么
  • 当我读到这篇文章时,我想:“不,我不想将它添加到我所有的viewDidLoads”中,它们只是两个超类。在我将它添加到其中一个之后,我运行了该应用程序,它对两者都有效......但不适用于-[AppDelegate applicationDidFinishLaunching:withOptions:]......不知道。
  • 注意现在是:[[UIBarButtonItem appearance] setTitleTextAttributes: attributes forState:];
【解决方案4】:

这是我在 iOS-7 上所做的事情

UIColor *red = [UIColor colorWithRed:165.0f/255.0f green:1.0f/255.0f blue:0.0f/255.0f alpha:1.0];
UIFont *font = [UIFont fontWithName:@"HelveticaNeue-Light" size:24.0f];

NSMutableDictionary *navBarTextAttributes = [NSMutableDictionary dictionaryWithCapacity:1];
[navBarTextAttributes setObject:font forKey:NSFontAttributeName];
[navBarTextAttributes setObject:red forKey:NSForegroundColorAttributeName ];

self.navBar.titleTextAttributes = navBarTextAttributes;

【讨论】:

    【解决方案5】:

    它在 Swift 中:

    let font = UIFont(name: "My_Font", size: 17.0)
    UINavigationBar.appearance().titleTextAttributes = [
        NSForegroundColorAttributeName: UIColor.whiteColor(),
        NSFontAttributeName: font!
    ]
    

    *请注意,您必须打开可选的 UIFont。

    【讨论】:

      【解决方案6】:

      对于 UIBarButons,使用 appearance proxy:

       NSDictionary *normalAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                            [UIFont fontWithName:@"Helvetica Neue" size:fontSize], UITextAttributeFont,
                                            nil];
          [[UIBarButtonItem appearance] setTitleTextAttributes:normalAttributes                                                                                                   
                                                      forState:UIControlStateNormal];
      

      如果您想限制您的样式影响哪些 UIBarButtonItems,请使用 appearanceWhenContainedIn: 代替。

      对于 UINavigationBar,你可以创建一个 UILabel 并将其设置为你的 navigationItem.titleView:

      UIabel *title = [[UILabel alloc] initWithFrame:CGRectZero];
      title.backgroundColor = [UIColor clearColor];
      title.font = [*yourfont*];
      title.textColor = [*your color*];
      self.navigationItem.titleView = title;
      

      【讨论】:

      • 外观代理对我也不起作用。你真的能够让字体改变吗?我也知道 UILabel 技巧,但这是现在在 iOS 7 中更改字体的唯一方法吗?在 iOS 6 中,我可以使用 titleTextAttributes 更改字体
      • 是的,我得到了外观代理,但它确实需要一些 jiggerypokery。我必须使用带有 2 个级别的容器的 appearanceWhenContainedIn - 即 [[UIBarButtonItem appearanceWhenContainedIn:[MyNavigationBar class],[MyNavigationController class],nil] ...
      • 注意 - 收容顺序是从后到前(至少从我的预期)......它是从内到外
      • 关于 UILabel,不确定它是否是唯一的方法,但它已经为 iOS 6 实现并且仍然适用于 7。
      【解决方案7】:

      以下是如何在 Swift 4 中全局更改导航栏标题的文本颜色:

      UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.foregroundColor.rawValue: UIColor.yellow]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-03-11
        • 1970-01-01
        • 1970-01-01
        • 2013-07-19
        • 2015-12-05
        • 2013-10-10
        • 1970-01-01
        相关资源
        最近更新 更多