Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 更改导航栏';s字体_Iphone_Objective C_Uinavigationcontroller_Uinavigationbar - Fatal编程技术网

Iphone 更改导航栏';s字体

Iphone 更改导航栏';s字体,iphone,objective-c,uinavigationcontroller,uinavigationbar,Iphone,Objective C,Uinavigationcontroller,Uinavigationbar,这个问题简单明了,但不幸的是答案并非如此 如何更改iOS 7及更高版本的UINavigationBar中文本的字体:: NSShadow* shadow = [NSShadow new]; shadow.shadowOffset = CGSizeMake(0.0f, 1.0f); shadow.shadowColor = [UIColor redColor]; [[UINavigationBar appearance] setTitleTextAttributes: @{ NSFore

这个问题简单明了,但不幸的是答案并非如此


如何更改iOS 7及更高版本的
UINavigationBar中文本的字体:

NSShadow* shadow = [NSShadow new];
shadow.shadowOffset = CGSizeMake(0.0f, 1.0f);
shadow.shadowColor = [UIColor redColor];
[[UINavigationBar appearance] setTitleTextAttributes: @{
     NSForegroundColorAttributeName: [UIColor greenColor],
                NSFontAttributeName: [UIFont fontWithName:@"Helvetica" size:20.0f],
              NSShadowAttributeName: shadow
                                                      }];
 [[UINavigationBar appearance] setTitleTextAttributes: @{
                                UITextAttributeTextColor: [UIColor greenColor],
                          UITextAttributeTextShadowColor: [UIColor redColor],
                         UITextAttributeTextShadowOffset: [NSValue valueWithUIOffset:UIOffsetMake(0.0f, 1.0f)],
                                     UITextAttributeFont: [UIFont fontWithName:@"Helvetica" size:20.0f]
     }];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 400, 44)];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:20.0];
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
label.textAlignment = UITextAlignmentCenter;
label.textColor =[UIColor whiteColor];
label.text=self.title;  
self.navigationItem.titleView = label;      
[label release];
来自iOS 5及更高版本:

NSShadow* shadow = [NSShadow new];
shadow.shadowOffset = CGSizeMake(0.0f, 1.0f);
shadow.shadowColor = [UIColor redColor];
[[UINavigationBar appearance] setTitleTextAttributes: @{
     NSForegroundColorAttributeName: [UIColor greenColor],
                NSFontAttributeName: [UIFont fontWithName:@"Helvetica" size:20.0f],
              NSShadowAttributeName: shadow
                                                      }];
 [[UINavigationBar appearance] setTitleTextAttributes: @{
                                UITextAttributeTextColor: [UIColor greenColor],
                          UITextAttributeTextShadowColor: [UIColor redColor],
                         UITextAttributeTextShadowOffset: [NSValue valueWithUIOffset:UIOffsetMake(0.0f, 1.0f)],
                                     UITextAttributeFont: [UIFont fontWithName:@"Helvetica" size:20.0f]
     }];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 400, 44)];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:20.0];
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
label.textAlignment = UITextAlignmentCenter;
label.textColor =[UIColor whiteColor];
label.text=self.title;  
self.navigationItem.titleView = label;      
[label release];
早于iOS 5:

NSShadow* shadow = [NSShadow new];
shadow.shadowOffset = CGSizeMake(0.0f, 1.0f);
shadow.shadowColor = [UIColor redColor];
[[UINavigationBar appearance] setTitleTextAttributes: @{
     NSForegroundColorAttributeName: [UIColor greenColor],
                NSFontAttributeName: [UIFont fontWithName:@"Helvetica" size:20.0f],
              NSShadowAttributeName: shadow
                                                      }];
 [[UINavigationBar appearance] setTitleTextAttributes: @{
                                UITextAttributeTextColor: [UIColor greenColor],
                          UITextAttributeTextShadowColor: [UIColor redColor],
                         UITextAttributeTextShadowOffset: [NSValue valueWithUIOffset:UIOffsetMake(0.0f, 1.0f)],
                                     UITextAttributeFont: [UIFont fontWithName:@"Helvetica" size:20.0f]
     }];
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 400, 44)];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:20.0];
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
label.textAlignment = UITextAlignmentCenter;
label.textColor =[UIColor whiteColor];
label.text=self.title;  
self.navigationItem.titleView = label;      
[label release];

上述答案是有效的。我将在最后一行之前添加以下一行。如果我没有,如果在左侧有一个后退按钮但没有右按钮,则标签的中心对齐不正确

...
[self.navigationItem.titleView sizeToFit]; 
[label release]; // not needed if you are using ARC

从iOS 5开始,您可以使用外观代理

答案是这个问题的副本:

针对iOS 7更新:

[[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:
                                                      [UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1.0], NSForegroundColorAttributeName,
                                                      shadow, NSShadowAttributeName,
                                                      [UIFont fontWithName:@"HelveticaNeue-CondensedBlack" size:21.0], NSFontAttributeName, nil]];
承蒙:


如果您想在Interface Builder中更改字体(无需任何代码),以下是在Xcode6中进行更改的方法:

1.)在导航控制器场景下找到导航栏视图

2.)在属性检查器中更改标题字体、颜色和阴影属性。

不确定为什么所有答案都包含阴影。添加操纵阴影的行对于更改文本字体没有任何作用。这两行代码适用于iOS 8.4和Swift

let attributesDictionary = [NSFontAttributeName: UIFont(name: "Helvetica Neue", size: 14)!]
navigationController!.navigationBar.titleTextAttributes = attributesDictionary

titleTextAttributes
存储一个字典,该字典将指定导航栏标题的字体、颜色、大小和其他属性

仅仅因为这是我在谷歌搜索这个主题时得到的第一个结果,而且有一个更新的答案:我确实相信这两个问题都有区别。当我要求更改单个导航栏的字体时,另一个问题是更改“所有”导航栏的字体。尽管如此,它还是一个有用的链接,因为其他人可能会感兴趣!对于单个导航栏,请使用以下内容:
[self.navigationController.navigationBar setTitleTextAttributes:@{UITextAttributeFont:[UIFont fontWithName:size::}]对于iOS7,您应使用NSFontAttributeName而不是UITextAttributeName。请提醒您,此答案已过时。在iOS 5和iOS 6中,有一些简单的方法可以自定义UI元素的外观。只需使用
外观
代理。如何让此代码使用SystemFont而不是对Helvetica进行硬编码?工作非常出色!(@Brabbeldas我确实把它改成了另一种字体:P)我已经下载了名为timeburner_regular.ttf的字体,所以我写了上面的代码。它对我很有用。文本的默认颜色将是
UINavigationBar
tintColor
是什么。如果需要不同的内容,请设置色调颜色,或将
NSForegroundColorAttributeName
属性和值添加到该属性字典中。如何使用SystemFont?