更改UINavigationBar字体属性?

时间:2012-01-08 01:02:32

标签: objective-c cocoa-touch fonts uinavigationbar title

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

如何在UINavigationBar中获得自定义标题?

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

7 个答案:

答案 0 :(得分:88)

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

[[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

答案 1 :(得分:21)

(使用新的iOS 5.0外观API无法实现这一点。)

修改

iOS> = 5.0:

设置导航栏的标题文本属性:

// 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。您只能通过将自定义标签设置为此标题视图来设置字体 您可以使用以下代码:(建议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];

答案 2 :(得分:7)

将它放在你的app delegate的

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

答案 3 :(得分:4)

在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()]
}

答案 4 :(得分:1)

以下链接可以帮助您。 这取决于您希望在应用程序

中的任何位置在当前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实例并使用所需的字体和其他属性填充它。 我完成了这个解决方案:

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;

答案 5 :(得分:0)

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

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

因此,在添加的自定义字体上也检查目标成员资格。

答案 6 :(得分:0)

对于iOS 13和Swift5。

要设置标题颜色和字体,只需在 viewDidLoad()中添加以下行:

UINavigationBar.appearance().titleTextAttributes = [
    .foregroundColor: UIColor.white, 
    .font: UIFont(name: "Arial", size: 24)! ]

用于设置条形按钮项目的文本颜色和字体:

UIBarButtonItem.appearance().setTitleTextAttributes([
    .foregroundColor: UIColor.white, 
    .font: UIFont(name: GetFontNameBold(), size: 40)! ],
    for: UIControl.State.normal)