How to set font & color of the title in UINavigati

2019-01-12 23:44发布

I have a multiple View Controllers and i want to set the font color of all to red.

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

is throwing an unrecognized selector error.

please give a code, to help fix the following.

7条回答
Explosion°爆炸
2楼-- · 2019-01-13 00:09

I did this by adding just few lines of code in AppDelegate.m class didFinishLaunchingWithOptions method: Use this code:

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

it works for me...

查看更多
兄弟一词,经得起流年.
3楼-- · 2019-01-13 00:10

Use this line of code

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

I think this will be helpful for you.

查看更多
Evening l夕情丶
4楼-- · 2019-01-13 00:14

For deployment targets greater than or equal to iOS 6, you should use NSShadow instead:

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

enter image description here

查看更多
SAY GOODBYE
5楼-- · 2019-01-13 00:21

Doing this on iOS 8+ and in Swift. There isn't a setTitleTextAttributes for the appearance object. Instead, do this:

UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName : AppTheme.fontWithSize(18)]
查看更多
一纸荒年 Trace。
6楼-- · 2019-01-13 00:23

If you need to do this in Swift, you can create an extension for the UINavigationBar to allow you to get or set these settings.

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

You can then set the color and font like this:

navigationBar.titleColor = UIColor.redColor()
navigationBar.titleFont = UIFont.systemFontOfSize(12)
查看更多
霸刀☆藐视天下
7楼-- · 2019-01-13 00:28

This could be used to set a custom view to a single navigationBar instead of a global setting

- (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;
}
查看更多
登录 后发表回答