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

如何设置字体&;使用iOS5外观API的UINavigationBar中标题的颜色?,ios5,uinavigationbar,uiappearance,Ios5,Uinavigationbar,Uiappearance,我有一个多视图控制器,我想设置所有的字体颜色为红色 [[UINavigationBar appearance] setFont:[UIFont boldSystemFontOfSize:12.0]]; 正在引发无法识别的选择器错误 如何修复此问题?使用这行代码 UILabel *badge_Label=[[UILabel alloc]initWithFrame:CGRectMake(5,3, 15, 15)]; badge_Label.backgroundColor=[UIColor red

我有一个多视图控制器,我想设置所有的字体颜色为红色

 [[UINavigationBar appearance] setFont:[UIFont boldSystemFontOfSize:12.0]];
正在引发无法识别的选择器错误

如何修复此问题?

使用这行代码

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];
我想这会对你有帮助。

来自雷·温德里奇:

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

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

对于大于或等于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];

我是通过在AppDelegate.m类中添加几行代码来实现的,类didfishLaunchingWithOptions方法: 使用此代码:

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

它对我很有用…

在iOS 8+和Swift上这样做。外观对象没有
setTitleTextAttributes
。相反,请执行以下操作:

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

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

- (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;
}
-(void)updateTitleWithString:(NSString*)标题
{
UIView*headerView=[[UIView alloc]initWithFrame:CGRectZero];
[headerView设置自动ResisingMask:UIViewAutoResisingFlexibleWidth];
[headerView setAutoresizesSubviews:是];
CGFloat headFontSize=(是系统设备还是IPAD?25.0f:19.0f);
UIFont*headFont=[UIFont boldSystemFontOfSize:headFontSize];
NSMutableParagraphStyle*style=[[NSParagraphStyle defaultParagraphStyle]mutableCopy];
[样式设置LineBreakMode:NSLineBreakByTruncingTail];
CGSize size=[标题边界矩形尺寸:CGSizeMake(190,headFontSize+6)选项:NSStringDrawingUserLineFragmentOrigin
属性:@{NSFontAttributeName:headFont,NSParagraphStyleAttributeName:style}上下文:nil].size;
headerView.frame=CGRectMake(0,0,size.width,self.navigationController.navigationBar.frame.size.height);
浮动标签高度=头部字体尺寸+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=是;
label.textAlignment=NSTextAlignmentCenter;
label.textColor=[UIColor-whiteColor];
label.font=headFont;
label.text=标题;
label.shadowColor=[uicolorWithWhite:0.0 alpha:0.4];
label.lineBreakMode=nslinebreakbytruncingtail;
label.shadowOffset=CGSizeMake(0,-1);
label.accessibilityLabel=@”;
[headerView addSubview:标签];
self.navigationItem.titleView=headerView;
}

如果需要在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)

然后你应该在导航栏上设置标签和视图,并设置字体颜色我知道,我尝试使用外观的代理功能已经超过1个小时了,我正在努力处理教程中的那部分代码,这导致在第一次推送后裁剪导航项的标题。。。注意字体大小,这是我目前的问题。。。显然大小0.0对我不起作用…新的字典文字将使它更漂亮,只是作为旁注,这只是5.0+而已,如果你支持4,那么如果([navBarInstance respondsToSelector:@selector(外观)],那么它值得做
if([navBarInstance respondsToSelector:@selector(外观)])
首先,因为它将使5以下的iOS版本崩溃。字体名称错误并使应用程序崩溃。尝试像Arial-BoldMT这样的方法。直到iOS 7中的
uitexttributetextcolor
被弃用,取而代之的是
NSForegroundColorAttributeName
,我想如果您单独使用
titleColor
titleFont
,这个解决方案是可行的。如果将它们一起使用,则每次调用其中一个变量时,
self.titleTextAttributes
都将设置为新值。setter在创建新字典时可能会得到另一个值。
navigationBar.titleColor = UIColor.redColor()
navigationBar.titleFont = UIFont.systemFontOfSize(12)