【问题标题】:change font of back button on uinavigationcontroller更改 uinavigationcontroller 上的后退按钮的字体
【发布时间】:2012-04-19 21:46:13
【问题描述】:

我正在尝试更改 UINavigationControllerBar 中后退按钮上文本的字体颜色

    [[UIBarButtonItem appearance] setTitleColor:[UIColor redColor] forState:UIControlStateNormal];

给我这个错误:[_UIBarItemAppearance setTitleColor:forState:]: unrecognized selector sent to instance 0x69aeb70'

有什么帮助吗?谢谢!

【问题讨论】:

    标签: fonts uinavigationcontroller back-button


    【解决方案1】:

    Swift 4 中的解决方案:

    UIBarButtonItem.appearance().setTitleTextAttributes(
    [
        NSAttributedStringKey.font: UIFont(name: "MyriadPro-SemiboldCond", size: 16)!,
        NSAttributedStringKey.foregroundColor: UIColor.white
    ], for: .normal)
    

    在 AppDelegate 中添加它,它将应用于应用中的所有按钮。

    【讨论】:

    • 似乎有一个错误,仅将字体应用于查看按钮的秒数。似乎在 iOS 12 中仍然存在。
    【解决方案2】:

    还有漂亮的解决方案,iOS7+(因为属性名称):

    NSShadow *shadow = [NSShadow new];
    [shadow setShadowColor: [UIColor colorWithWhite:0.0f alpha:0.750f]];
    [shadow setShadowOffset: CGSizeMake(0.0f, 1.0f)];
    
    [[UIBarButtonItem appearance] setTitleTextAttributes:@{
            NSFontAttributeName: [UIFont systemFontOfSize:24],
            NSForegroundColorAttributeName: [UIColor colorWithWhite:0.2 alpha:1.0],
            NSShadowAttributeName: shadow,
    } forState:UIControlStateNormal];
    

    【讨论】:

      【解决方案3】:
      [[UIBarButtonItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                           [UIFont fontWithName:kDefaultFont size:16.0f],UITextAttributeFont,
                                                           nil] forState:UIControlStateNormal];
      

      【讨论】:

      • 对于IOS7,将UITextAttributeFont替换为NSFontAttributeName
      • 在 iOS8 上完美运行,我也将 UITextAttributeFont 更改为 NSFontAttributeName,它也可以正常工作
      【解决方案4】:

      改用这个,默认函数在 ios 5 中可用

      UIBarButtonItem *backbutton =  [[UIBarButtonItem alloc] initWithTitle:@"back" style:UIBarButtonItemStyleBordered target:nil action:nil];    
      
          [backbutton setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                         [UIColor blackColor],UITextAttributeTextColor,[UIFont fontWithName:TEXTFONT size:16.0f],UITextAttributeFont,
                                                         nil] forState:UIControlStateNormal]; 
      

      【讨论】:

      • 不适用于 iOS7+。 Itgiawa 的答案是为它工作。
      【解决方案5】:
      NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
      [attributes setValue:[UIColor colorWithRed:(163.0f/255.0f) green:(0.0f) blue:(0.0f) alpha:1.0f] forKey:UITextAttributeTextColor];
      [attributes setValue:[UIColor clearColor] forKey:UITextAttributeTextShadowColor];
      [attributes setValue:[NSValue valueWithUIOffset:UIOffsetMake(0.0, 0.0)] forKey:UITextAttributeTextShadowOffset];
      [[UIBarButtonItem appearance] setTitleTextAttributes:attributes forState:UIControlStateNormal];
      

      似乎有效!

      【讨论】:

      • 使用外观选项的更好解决方案。它们在全局范围内工作,或按类工作,或按实例工作。任何来这里的人都应该改用这个答案。
      • 完全取决于具体情况。我正在开发一款游戏,只需要暂时更改字体。 ;-)
      • [[UIBarButtonItem 外观] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0], NSForegroundColorAttributeName, [UIFont fontWithName:@"Cuprum -regular" size:14.0], NSFontAttributeName, nil] forState:UIControlStateNormal];
      • 它的更坝完美地改变了字体
      猜你喜欢
      • 2014-01-31
      • 2012-12-27
      • 2011-05-04
      • 1970-01-01
      • 1970-01-01
      • 2017-03-04
      • 1970-01-01
      • 2015-10-11
      • 2012-04-09
      相关资源
      最近更新 更多