首页 文章

更改UINavigationBar字体属性?

提问于
浏览
42

我在UIViewController视图中添加了UINavigationBar . 我想更改字体属性 . 请注意,我想更改UINavigationBar而不是控制器 . 在我使用UINavigationController的应用程序中,我使用 self.navigationItem.titleView = label; 来显示自定义标签 .

如何在UINavigationBar中获得自定义 Headers ?

P.S我用它来设置我的UINavigationBar的 Headers 文本 self.navBar.topItem.title = @"Information"; .

6 回答

  • 21

    在iOS8 swift中,使用以下代码设置字体:

    var navigationBarAppearance = UINavigationBar.appearance()
    let font = UIFont(name: "Open Sans", size: 17)
    if let font = font {
        navigationBarAppearance.titleTextAttributes = [NSFontAttributeName: font, NSForegroundColorAttributeName: UIColor.whiteColor()]
    }
    
  • 88

    (使用新的iOS 5.0 Appearance API无法实现) .

    Edit:

    iOS> = 5.0:

    设置导航栏的 Headers 文本属性:

    // 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没有名为label之类的属性,只有titleView . 您只能通过将自定义标签设置为此 Headers 视图来设置字体您可以使用以下代码:(如建议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];
    
  • 1

    把它放在你的app代表中

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

    来自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]];
    
  • 7

    从iOS 5开始,我们必须使用titleTextAttribute Dictionary(UInavigation控制器类引用中的预定义字典)设置 Headers 文本颜色和导航栏的字体 .

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

    以下教程是UIElements自定义的最佳教程,如UInavigation bar,UIsegmented control,UITabBar . 这可能对您有所帮助

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

  • 4

    不要忘记将字体添加到目标 .

    我做了一切关于字体的事情我无法加载它,实际上字体不是我的目标成员 .

    因此,在添加的自定义字体上也检查目标成员身份 .

  • 0

    以下链接将帮助您 . 这取决于您希望在应用程序的任何位置在当前viewController的导航栏上设置字体属性的位置

    你可以在这里找到很好的教程%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

    基本思想是创建NSDictionary实例并用您想要的字体和其他属性填充它 . 我完成了这个解决方案:

    在[super viewDidLoad]之后的-viewDidLoad控制器方法中放入以下行:

    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;
    

相关问题