titleTextAttributes UIAppearance font in iOS 7

前端 未结 7 1504
醉酒成梦
醉酒成梦 2020-12-08 09:47

I am using UIAppearance to apply fonts to UINavigationBar and UIBarButtonItem and I am having problems. I ran this code:

[[UIBarButtonItem appearanceWhenCont         


        
相关标签:
7条回答
  • 2020-12-08 10:25

    Here it is in Swift:

    let font = UIFont(name: "My_Font", size: 17.0)
    UINavigationBar.appearance().titleTextAttributes = [
        NSForegroundColorAttributeName: UIColor.whiteColor(),
        NSFontAttributeName: font!
    ]
    

    *Note that you have to unwrap the optional UIFont.

    0 讨论(0)
  • 2020-12-08 10:28
    NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont 
        fontWithName:@"YOURFONT" size:14], NSFontAttributeName, 
        [UIColor whiteColor], NSForegroundColorAttributeName, nil];
    
    [[UINavigationBar appearance] setTitleTextAttributes:attributes];
    

    The key is to use NSFontAttributeName and so forth. I assume they are moving over to using the NS variety for 64-bit compatibility. The above code worked on my iOS7 device.

    0 讨论(0)
  • 2020-12-08 10:46

    For the UIBarButons use the appearance proxy:

     NSDictionary *normalAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                          [UIFont fontWithName:@"Helvetica Neue" size:fontSize], UITextAttributeFont,
                                          nil];
        [[UIBarButtonItem appearance] setTitleTextAttributes:normalAttributes                                                                                                   
                                                    forState:UIControlStateNormal];
    

    If you want to limit which UIBarButtonItems your style affects use appearanceWhenContainedIn: instead.

    For the UINavigationBar, you can create a UILabel and set it to your navigationItem.titleView:

    UIabel *title = [[UILabel alloc] initWithFrame:CGRectZero];
    title.backgroundColor = [UIColor clearColor];
    title.font = [*yourfont*];
    title.textColor = [*your color*];
    self.navigationItem.titleView = title;
    
    0 讨论(0)
  • 2020-12-08 10:47

    Here's what I do on iOS-7

    UIColor *red = [UIColor colorWithRed:165.0f/255.0f green:1.0f/255.0f blue:0.0f/255.0f alpha:1.0];
    UIFont *font = [UIFont fontWithName:@"HelveticaNeue-Light" size:24.0f];
    
    NSMutableDictionary *navBarTextAttributes = [NSMutableDictionary dictionaryWithCapacity:1];
    [navBarTextAttributes setObject:font forKey:NSFontAttributeName];
    [navBarTextAttributes setObject:red forKey:NSForegroundColorAttributeName ];
    
    self.navBar.titleTextAttributes = navBarTextAttributes;
    
    0 讨论(0)
  • 2020-12-08 10:50

    Following @Alex Zavatone's answer - it could be done nicely in just one line of code:

    self.navBar.titleTextAttributes = @{
        NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue-Light" size:20.0], 
        NSForegroundColorAttributeName: [UIColor redColor]
    };
    
    0 讨论(0)
  • 2020-12-08 10:50

    This worked fine for iOS 7 when I moved it to a viewDidLoad method in my view controller.

    [[UIBarButtonItem appearance] setTitleTextAttributes:attributes];
    

    When I tried to do this in AppDelegate it didn't work, though[[UINavigationBar appearance] setTitleTextAttributes:setTitleTextAttributes:attributes]; worked fine in AppDelegate. Also it should go before you create and assign the bar button.

    It also works from initWithCoder method if you create your bar button on Storyboard.

    Update: If you change deployment target to 7.0 then Xcode will provide you with warnings saying that for example UITextAttributeTextColor is deprecated and you should use NSForegroundColorAttributeName or instead of UITextAttributeFont use NSFontAttributeName. After changing attribute constants you can call setTitleTextAttributes: from AppDelegate.

    0 讨论(0)
提交回复
热议问题