如何使用iOS5外观API在UINavigationBar中设置标题的字体和颜色?


90

我有多个View Controller,并且要将所有字体的颜色设置为红色。

 [[UINavigationBar appearance] setFont:[UIFont boldSystemFontOfSize:12.0]];

抛出无法识别的选择器错误。

我怎样才能解决这个问题?


然后,您应该设置标签并在导航栏上查看并设置字体颜色
vishiphone

Answers:


207

从雷·温德利希(Ray Wenderlich):

http://www.raywenderlich.com/4344/user-interface-customization-in-ios-5

// Customize the title text for *all* UINavigationBars
[[UINavigationBar appearance] setTitleTextAttributes:
    [NSDictionary dictionaryWithObjectsAndKeys:
        [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0], 
        UITextAttributeTextColor, 
        [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8], 
        UITextAttributeTextShadowColor, 
        [NSValue valueWithUIOffset:UIOffsetMake(0, -1)], 
        UITextAttributeTextShadowOffset, 
        [UIFont fontWithName:@"Arial-Bold" size:0.0], 
        UITextAttributeFont, 
        nil]];

或者,如果您更喜欢对象文字样式:

[[UINavigationBar appearance] setTitleTextAttributes:@{
    UITextAttributeTextColor: [UIColor colorWithRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:1.0],
    UITextAttributeTextShadowColor: [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8],
    UITextAttributeTextShadowOffset: [NSValue valueWithUIOffset:UIOffsetMake(0, -1)],
    UITextAttributeFont: [UIFont fontWithName:@"Arial-Bold" size:0.0],
}];

针对iOS 7及以下版本进行编辑

UITextAttributes不赞成使用iOS 7,您可以使用以下命令:

NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor colorWithWhite:.0f alpha:1.f];
shadow.shadowOffset = CGSizeMake(0, -1);

[[UINavigationBar appearance] setTitleTextAttributes:@{
     NSForegroundColorAttributeName: [UIColor whiteColor],
     NSShadowAttributeName: shadow,
     NSFontAttributeName: [UIFont fontWithName:@"Arial-Bold" size:15.0f]
     }];

1
在一个多小时的时间里,我在教程中的那部分代码上苦苦挣扎,这导致在第一次按下后就裁剪了导航项的标题。要小心字体大小,这是我所遇到的问题。 0.0不适用于我...
Bartu

3
新的字典文字将使它更漂亮
tybro0103

正如一个侧面说明,这是5.0+而已,如果你支持4则是值得做的if ([navBarInstance respondsToSelector:@selector(appearance)])第一,因为它会崩溃到低于5的iOS版本
亚当韦特

1
字体名称错误,导致应用程序崩溃。尝试类似Arial-BoldMT的方法。
MacMark 2013年

17
TIL UITextAttributeTextColor已被NSForegroundColorAttributeNameiOS 7 弃用
布伦登

23

对于大于或等于iOS 6的部署目标,应NSShadow改用:

NSShadow * shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor lightGrayColor];
shadow.shadowOffset = CGSizeMake(0, -2);

NSDictionary * navBarTitleTextAttributes =
@{ NSForegroundColorAttributeName : [UIColor redColor],
   NSShadowAttributeName          : shadow,
   NSFontAttributeName            : [UIFont systemFontOfSize:14] };

[[UINavigationBar appearance] setTitleTextAttributes:navBarTitleTextAttributes];

在此处输入图片说明


19

在iOS 8+和Swift中执行此操作。没有一个setTitleTextAttributes对外观对象。相反,请执行以下操作:

UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName : AppTheme.fontWithSize(18)]

5

我是通过在AppDelegate.m类didFinishLaunchingWithOptions方法中添加几行代码来做到这一点的:使用此代码:

NSDictionary *navbarTitleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                           [UIColor colorWithRed:255.0f/255.0f green:0.0f/255.0f blue:0.0f/255.0f alpha:1.0],UITextAttributeTextColor,
                                           [UIColor clearColor], UITextAttributeTextShadowColor,
                                           [NSValue valueWithUIOffset:UIOffsetMake(-1, 0)], UITextAttributeTextShadowOffset, nil];

[[UINavigationBar appearance] setTitleTextAttributes:navbarTitleTextAttributes];

这个对我有用...


5

如果需要在Swift中执行此操作,则可以为UINavigationBar创建扩展,以允许您获取或设置这些设置。

extension UINavigationBar {
var titleColor: UIColor? {
    get {
        if let attributes = self.titleTextAttributes {
            return attributes[NSForegroundColorAttributeName] as? UIColor
        }
        return nil
    }
    set {
        if let value = newValue {
            self.titleTextAttributes = [NSForegroundColorAttributeName: value]
        }
    }
}

var titleFont: UIFont? {
    get {
        if let attributes = self.titleTextAttributes {
            return attributes[NSFontAttributeName] as? UIFont
        }
        return nil
    }
    set {
        if let value = newValue {
            self.titleTextAttributes = [NSFontAttributeName: value]
        }
    }
    }
}

然后可以像这样设置颜色和字体:

navigationBar.titleColor = UIColor.redColor()
navigationBar.titleFont = UIFont.systemFontOfSize(12)

我想如果您单独使用titleColortitleFont单独使用,此解决方案都可以。如果将它们一起使用,则self.titleTextAttributes每次调用其中一个变量时,都会将设置为新值。设置器在创建新字典时可能会获得其他值。
user023

1

这可用于将自定义视图设置为单个navigationBar而不是全局设置

- (void)updateTitleWithString:(NSString *)title
{
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectZero];
    [headerView setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
    [headerView setAutoresizesSubviews:YES];

    CGFloat headFontSize = (IS_SYSTEM_DEVICE_IPAD ? 25.0f : 19.0f);
    UIFont *headFont = [UIFont boldSystemFontOfSize: headFontSize ];

    NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
    [style setLineBreakMode:NSLineBreakByTruncatingTail];

    CGSize size = [title boundingRectWithSize:CGSizeMake(190,headFontSize + 6) options:NSStringDrawingUsesLineFragmentOrigin
                                   attributes:@{NSFontAttributeName : headFont, NSParagraphStyleAttributeName : style} context:nil].size;

    headerView.frame  = CGRectMake(0, 0,size.width,self.navigationController.navigationBar.frame.size.height);
    float labelHeight = headFontSize + 6;
    float labelYLoc   = (   self.navigationController.navigationBar.frame.size.height - labelHeight ) / 2;
    UILabel *label    = [[UILabel alloc] initWithFrame:CGRectMake(0,labelYLoc, size.width,labelHeight)];
    label.backgroundColor = [UIColor clearColor];
    label.adjustsFontSizeToFitWidth = YES;
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [UIColor whiteColor];
    label.font = headFont;
    label.text = title;
    label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.4];
    label.lineBreakMode = NSLineBreakByTruncatingTail;
    label.shadowOffset = CGSizeMake(0,-1);
    label.accessibilityLabel = @"<LABEL>";
    [headerView addSubview:label];

    self.navigationItem.titleView = headerView;
}

-5

使用此行代码

UILabel *badge_Label=[[UILabel alloc]initWithFrame:CGRectMake(5,3, 15, 15)];
badge_Label.backgroundColor=[UIColor redcolor];
badge_Label.font=[UIFont systemFontOfSize:12];
[badge_Label setText:@"20"];
[self.navigationController.navigationBar addSubview:badgelabel];

我认为这将对您有所帮助。


我知道这一点,我正在尝试使用外观API的代理功能
carbonr
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.