在uinavigationcontroller上更改后退按钮的字体

时间:2012-04-06 07:51:49

标签: fonts uinavigationcontroller back-button

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

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

给我这个错误:[_ UIBarItemAppearance setTitleColor:forState:]:无法识别的选择器发送到实例0x69aeb70'

有任何帮助吗?谢谢!

5 个答案:

答案 0 :(得分:50)

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];

似乎有效!

答案 1 :(得分:17)

[[UIBarButtonItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                     [UIFont fontWithName:kDefaultFont size:16.0f],UITextAttributeFont,
                                                     nil] forState:UIControlStateNormal];

答案 2 :(得分:15)

使用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]; 

答案 3 :(得分:6)

漂亮的解决方案,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];

答案 4 :(得分:2)

Swift 4中的解决方案:

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

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