Change UINavigationBar font properties?

2020-02-17 06:25发布

I have an UINavigationBar added to my UIViewController view. I want to change the fonts properties. Note that I want to change a UINavigationBar not controller. In my app where I use UINavigationController I use self.navigationItem.titleView = label; to display the custom label.

How can I accomplish to have a custom title in my UINavigationBar?

P.S I use this to set the title text self.navBar.topItem.title = @"Information"; of my UINavigationBar.

6条回答
何必那么认真
2楼-- · 2020-02-17 06:37

In iOS8 swift, use the following code to set font:

var navigationBarAppearance = UINavigationBar.appearance()
let font = UIFont(name: "Open Sans", size: 17)
if let font = font {
    navigationBarAppearance.titleTextAttributes = [NSFontAttributeName: font, NSForegroundColorAttributeName: UIColor.whiteColor()]
}
查看更多
成全新的幸福
3楼-- · 2020-02-17 06:40

Don't forget to add the font to your target.

I did everything that can be done about a font and I couldn't load it, when , in fact the font wasn't a member of my target.

So check Target Membership as well on the custom font added.

查看更多
女痞
4楼-- · 2020-02-17 06:41

From iOS 5 onwards we have to set title text color and font of navigation bar using titleTextAttribute Dictionary(predefined dictionary in UInavigation controller class reference).

[[UINavigationBar appearance] setTitleTextAttributes: 
    [NSDictionary dictionaryWithObjectsAndKeys: 
        [UIColor blackColor], NSForegroundColorAttributeName, 
           [UIFont fontWithName:@"ArialMT" size:16.0], NSFontAttributeName,nil]];

The below tutorial is the best tutorial for customization of UIElements like UInavigation bar, UIsegmented control, UITabBar. This may be helpful to you

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

查看更多
爷、活的狠高调
5楼-- · 2020-02-17 06:44

link below will help you. It depends on where would you like to set font properties on current viewController's navigation bar all anywhere in application

you can find nice tutorial here% http://www.appcoda.com/customize-navigation-status-bar-ios-7/?utm_campaign=iOS_Dev_Weekly_Issue_118&utm_medium=email&utm_source=iOS%2BDev%2BWeekly

The basic idea is to create NSDictionary instance and fill it with your desired font and other properties. I finished with this solution:

in your -viewDidLoad controller method after [super viewDidLoad] put this lines:

UIColor *color = [UIColor redColor];
NSShadow *shadow = [NSShadow new];
UIFont *font = [UIFont fontWithName:@"EnterYourFontName" size:20.0]
shadow.shadowColor = [UIColor greenColor];
shadow.shadowBlurRadius = 2;
shadow.shadowOffset = CGSizeMake(1.0f, 1.0f);

//fill this dictionary with text attributes
NSMutableDictionary *topBarTextAttributes = [NSMutableDictionary new];
    //ios 7
topBarTextAttributes[NSForegroundColorAttributeName] = color;
    //ios 6
topBarTextAttributes[UITextAttributeTextColor] = color;
    //ios 6
topBarTextAttributes[UITextAttributeTextShadowOffset] = [NSValue valueWithCGSize:shadow.shadowOffset];
topBarTextAttributes[UITextAttributeTextShadowColor] = shadow.shadowColor;
    //ios 7
topBarTextAttributes[NSShadowAttributeName] = shadow;
    //ios 6
topBarTextAttributes[UITextAttributeFont]   = font;
    //ios 7
topBarTextAttributes[NSFontAttributeName]   = font;

//for all the controllers uncomment this line
//    [[UINavigationBar appearance]setTitleTextAttributes:topBarTextAttributes];

//for current controller uncoment this line
//    self.navigationController.navigationBar.titleTextAttributes = topBarTextAttributes;
查看更多
混吃等死
6楼-- · 2020-02-17 06:52

(This is not possible using the new iOS 5.0 Appearance API).

Edit:

iOS >= 5.0 :

Set the Title Text Attributes for the Navigationbar:

// Customize the title text for *all* UINavigationBars
NSDictionary *settings = @{
    UITextAttributeFont                 :  [UIFont fontWithName:@"YOURFONTNAME" size:20.0],
    UITextAttributeTextColor            :  [UIColor whiteColor],
    UITextAttributeTextShadowColor      :  [UIColor clearColor],
    UITextAttributeTextShadowOffset     :  [NSValue valueWithUIOffset:UIOffsetZero]};

[[UINavigationBar appearance] setTitleTextAttributes:settings];

iOS < 5.0

UINavigationItem doesn't have a property called label or something, only a titleView. You are only able to set the font by setting a custom label as this title view You could use the following code: (as suggested here)

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
label.font = [UIFont fontWithName:@"YOURFONTNAME" size:20.0];
label.shadowColor = [UIColor clearColor];
label.textColor =[UIColor whiteColor];
label.text = self.title;  
self.navigationItem.titleView = label;      
[label release];
查看更多
霸刀☆藐视天下
7楼-- · 2020-02-17 07:01

Just put this in your app delegate's

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

From Ray Wenderlich:

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

[[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:@"STHeitiSC-Light" size:0.0], 
UITextAttributeFont, nil]];
查看更多
登录 后发表回答