簡體   English   中英

iPhone導航欄標題文字顏色

[英]iPhone Navigation Bar Title text color

默認情況下,iOS導航欄標題顏色似乎是白色。 有沒有辦法將其改為不同的顏色?

我知道使用圖像的navigationItem.titleView方法。 由於我的設計技巧有限,而且我沒有獲得標准光面,我更喜歡更改文字顏色。

任何見解都會非常感激。

現代的方法

現代方式,對於整個導航控制器......在加載導航控制器的根視圖時執行此操作一次。

[self.navigationController.navigationBar setTitleTextAttributes:
   @{NSForegroundColorAttributeName:[UIColor yellowColor]}];

但是,這似乎對后續視圖沒有影響。

經典的方法

舊的方式,每個視圖控制器(這些常量適用於iOS 6,但如果想在iOS 7外觀上按照視圖控制器進行操作,您將需要相同的方法但具有不同的常量):

您需要使用UILabel作為navigationItemtitleView

標簽應該:

  • 有一個清晰的背景顏色( label.backgroundColor = [UIColor clearColor] )。
  • 使用粗體label.font = [UIFont boldSystemFontOfSize: 20.0f]系統字體( label.font = [UIFont boldSystemFontOfSize: 20.0f] )。
  • 有50%alpha的黑色陰影( label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5] )。
  • 您還需要將文本對齊設置為居中( label.textAlignment = NSTextAlignmentCenter (舊版SDK的UITextAlignmentCenter )。

將標簽文本顏色設置為您想要的任何自定義顏色。 您確實需要一種不會導致文本混合成陰影的顏色,這種顏色難以閱讀。

我通過反復試驗解決了這個問題,但我想出的價值最終太簡單了,以至於他們不能成為蘋果選擇的。 :)

如果要驗證這一點,請將此代碼放入Apple的NavBar示例的 PageThreeViewController.minitWithNibName:bundle:中。 這將用黃色標簽替換文本。 除了顏色之外,這應該與Apple代碼生成的原始文件無法區分。

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self)
    {
        // this will appear as the title in the navigation bar
        UILabel *label = [[[UILabel alloc] initWithFrame:CGRectZero] autorelease];
        label.backgroundColor = [UIColor clearColor];
        label.font = [UIFont boldSystemFontOfSize:20.0];
        label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
        label.textAlignment = NSTextAlignmentCenter;
                           // ^-Use UITextAlignmentCenter for older SDKs.
        label.textColor = [UIColor yellowColor]; // change this color
        self.navigationItem.titleView = label;
        label.text = NSLocalizedString(@"PageThreeTitle", @"");
        [label sizeToFit];
    }

    return self;
}

編輯:另外,閱讀Erik B的答案如下。 我的代碼顯示了效果,但是他的代碼提供了一種更簡單的方法來將其放在現有視圖控制器上。

我知道這是一個非常古老的線程,但我認為知道新用戶iOS 5帶來了一個用於建立標題屬性的新屬性會很有用。

您可以使用UINavigationBar的setTitleTextAttributes來設置字體,顏色,偏移和陰影顏色。

此外,您可以為整個應用程序中的所有UINavigationBars設置相同的默認UINavigationBar標題文本屬性。

例如:

NSDictionary *navbarTitleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                            [UIColor whiteColor],UITextAttributeTextColor, 
                                            [UIColor blackColor], UITextAttributeTextShadowColor, 
                                            [NSValue valueWithUIOffset:UIOffsetMake(-1, 0)], UITextAttributeTextShadowOffset, nil];

[[UINavigationBar appearance] setTitleTextAttributes:navbarTitleTextAttributes];

在iOS 5中,您可以通過以下方式更改navigationBar標題顏色:

navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor yellowColor]};

根據Steven Fisher的回答,我寫了這段代碼:

- (void)setTitle:(NSString *)title
{
    [super setTitle:title];
    UILabel *titleView = (UILabel *)self.navigationItem.titleView;
    if (!titleView) {
        titleView = [[UILabel alloc] initWithFrame:CGRectZero];
        titleView.backgroundColor = [UIColor clearColor];
        titleView.font = [UIFont boldSystemFontOfSize:20.0];
        titleView.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];

        titleView.textColor = [UIColor yellowColor]; // Change to desired color

        self.navigationItem.titleView = titleView;
        [titleView release];
    }
    titleView.text = title;
    [titleView sizeToFit];
}

除了正確處理框架之外,此代碼的優點是,如果更改控制器的標題,自定義標題視圖也將更新。 無需手動更新。

另一個很大的優點是它可以很容易地啟用自定義標題顏色。 您需要做的就是將此方法添加到控制器。

以上大多數建議現已棄用,iOS 7使用 -

NSDictionary *textAttributes = [NSDictionary dictionaryWithObjectsAndKeys: 
                               [UIColor whiteColor],NSForegroundColorAttributeName, 
                               [UIColor whiteColor],NSBackgroundColorAttributeName,nil];

self.navigationController.navigationBar.titleTextAttributes = textAttributes;
self.title = @"Title of the Page";

另外,檢查NSAttributedString.h以獲取可以設置的各種文本屬性。

在IOS 7和8中,您可以更改標題的顏色,讓我們說綠色

self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor greenColor] forKey:NSForegroundColorAttributeName];

為了使問題保持​​最新,我將添加Alex RR解決方案,但在Swift中

self.navigationController.navigationBar.barTintColor = .blueColor()
self.navigationController.navigationBar.tintColor = .whiteColor()
self.navigationController.navigationBar.titleTextAttributes = [
    NSForegroundColorAttributeName : UIColor.whiteColor()
]

結果是:

在此輸入圖像描述

方法1 ,在IB中設置:

在此輸入圖像描述

方法2 ,一行代碼:

navigationController?.navigationBar.barTintColor = UIColor.blackColor()

從iOS 5開始,我們必須使用titleTextAttribute Dictionary(UInavigation控制器類引用中的預定義字典)設置標題文本顏色和導航欄的字體。

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

如果您嘗試更改頁面上的顏色,tewha的解決方案效果很好,但我希望能夠更改每個頁面上的顏色。 我做了一些小修改,以便它適用於UINavigationController上的所有頁面

NavigationDelegate.h

//This will change the color of the navigation bar
#import <Foundation/Foundation.h>
@interface NavigationDelegate : NSObject<UINavigationControllerDelegate> {
}
@end

NavigationDelegate.m

#import "NavigationDelegate.h"
@implementation NavigationDelegate

- (void)navigationController:(UINavigationController *)navigationController 
      willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
    CGRect frame = CGRectMake(0, 0, 200, 44);//TODO: Can we get the size of the text?
    UILabel* label = [[[UILabel alloc] initWithFrame:frame] autorelease];
    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 yellowColor];
    //The two lines below are the only ones that have changed
    label.text=viewController.title;
    viewController.navigationItem.titleView = label;
}
@end

Swift版本

我發現大多數人都提出了Objective_C版本的答案

我想通過使用Swift為任何需要它的人實現這個功能。

在ViewDidload中

1.使NavigationBar背景變為彩色(例如:BLUE)

self.navigationController?.navigationBar.barTintColor = UIColor.blueColor()

2.使NavigationBar背景成為圖像(例如:ABC.png)

let barMetrix = UIBarMetrics(rawValue: 0)!

self.navigationController?.navigationBar
      .setBackgroundImage(UIImage(named: "ABC"), forBarMetrics: barMetrix)

3.更改NavigationBar標題(例如:[字體:Futura,10] [顏色:紅色])

navigationController?.navigationBar.titleTextAttributes = [
            NSForegroundColorAttributeName : UIColor.redColor(),
            NSFontAttributeName : UIFont(name: "Futura", size: 10)!
        ]

(提示1:不要忘記UIFont之后的“!”標記)

(提示2:標題文本有很多屬性,命令點擊“NSFontAttributeName”你可以進入類並查看keyNames和他們需要的Objects類型)

我希望我能幫忙!:D

在任何視圖控制器viewDidLoad或viewWillAppear方法中使用以下代碼。

- (void)viewDidLoad
{
    [super viewDidLoad];

    //I am using UIColor yellowColor for an example but you can use whatever color you like   
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor yellowColor]};

    //change the title here to whatever you like
    self.title = @"Home";
    // Do any additional setup after loading the view.
}

簡短又甜蜜。

[[[self navigationController] navigationBar] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor redColor]}];

這是我基於史蒂文斯的解決方案

唯一真正的不同之處在於,如果根據文字長度的話,我會根據文字長度調整位置,這似乎與蘋果的做法類似

UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(([self.title length] < 10 ? UITextAlignmentCenter : UITextAlignmentLeft), 0, 480,44)];
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.font = [UIFont boldSystemFontOfSize: 20.0f];
titleLabel.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
titleLabel.textAlignment = ([self.title length] < 10 ? UITextAlignmentCenter : UITextAlignmentLeft);
titleLabel.textColor = [UIColor redColor];
titleLabel.text = self.title;
self.navigationItem.titleView = titleLabel;
[titleLabel release];

您可能需要根據字體大小調整10值

建議設置self.title,因為在推送子導航欄或在tabbars上顯示標題時使用它。

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // create and customize title view
        self.title = NSLocalizedString(@"My Custom Title", @"");
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        titleLabel.text = self.title;
        titleLabel.font = [UIFont boldSystemFontOfSize:16];
        titleLabel.backgroundColor = [UIColor clearColor];
        titleLabel.textColor = [UIColor whiteColor];
        [titleLabel sizeToFit];
        self.navigationItem.titleView = titleLabel;
        [titleLabel release];
    }
}

這是一個非常古老的線程,但我想為iOS 7及更高版本提供設置導航欄標題的顏色,大小和垂直位置的答案

顏色和尺寸

 NSDictionary *titleAttributes =@{
                                NSFontAttributeName :[UIFont fontWithName:@"Helvetica-Bold" size:14.0],
                                NSForegroundColorAttributeName : [UIColor whiteColor]
                                };

對於垂直位置

[[UINavigationBar appearance] setTitleVerticalPositionAdjustment:-10.0 forBarMetrics:UIBarMetricsDefault];

設置標題並分配屬性字典

[[self navigationItem] setTitle:@"CLUBHOUSE"];
self.navigationController.navigationBar.titleTextAttributes = titleAttributes;

我的導航按鈕將文本從中心拋出(當你只有一個按鈕時)我遇到了問題。 為了解決這個問題,我只是改變了我的幀大小:

CGRect frame = CGRectMake(0, 0, [self.title sizeWithFont:[UIFont boldSystemFontOfSize:20.0]].width, 44);

我已經自定義了navigationBar的背景圖像和左按鈕項,灰色標題不適合背景。 然后我用:

[self.navigationBar setTintColor:[UIColor darkGrayColor]];

將色調顏色更改為灰色。 現在標題是白色的! 這就是我想要的。

希望也能幫助:)

這在Swift中對我有用:

navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.white]
self.navigationItem.title=@"Extras";
[self.navigationController.navigationBar setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"HelveticaNeue" size:21], NSFontAttributeName,[UIColor whiteColor],UITextAttributeTextColor,nil]];

使用新的iOS 7文本屬性和現代物鏡c更新Alex RR的帖子以減少噪音:

NSShadow *titleShadow = [[NSShadow alloc] init];
titleShadow.shadowColor = [UIColor blackColor];
titleShadow.shadowOffset = CGSizeMake(-1, 0);
NSDictionary *navbarTitleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor],
                                            NSShadowAttributeName:titleShadow};

[[UINavigationBar appearance] setTitleTextAttributes:navbarTitleTextAttributes];

像這樣使用Orientation支持

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,40)];
[view setBackgroundColor:[UIColor clearColor]];
[view setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight ];

UILabel *nameLabel = [[UILabel alloc] init];
[nameLabel setFrame:CGRectMake(0, 0, 320, 40)];
[nameLabel setBackgroundColor:[UIColor clearColor]];
[nameLabel setAutoresizingMask:UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin |UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin];
[nameLabel setTextColor:[UIColor whiteColor]];
[nameLabel setFont:[UIFont boldSystemFontOfSize:17]];
[nameLabel setText:titleString];
[nameLabel setTextAlignment:UITextAlignmentCenter];
[view addSubview:nameLabel];
[nameLabel release];
self.navigationItem.titleView = view;
[view release];

設置標題的字體大小我使用了以下條件..也許對任何人都有幫助

if ([currentTitle length]>24) msize = 10.0f;
    else if ([currentTitle length]>16) msize = 14.0f;
    else if ([currentTitle length]>12) msize = 18.0f;

我確實認為設置UINavigationBar顏色的正確方法是:

NSDictionary *attributes=[NSDictionary dictionaryWithObjectsAndKeys:[UIColor redColor],UITextAttributeTextColor, nil];
self.titleTextAttributes = attributes;

上面的代碼是在UINavigationBar上編寫的子類,顯然也可以在沒有子類化的情況下工作。

Swift 4版本:

self.navigationController.navigationBar.titleTextAttributes = [
        NSAttributedStringKey.foregroundColor: UIColor.green]

遇到與我們在navBar中插入按鈕時移動的標簽相同的問題(如同其他)(在我的情況下,我有一個微調器,我在加載日期時用按鈕替換),上述解決方案不起作用對我來說,所以這里是有效的,並始終將標簽保持在同一個地方:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
    // this will appear as the title in the navigation bar
    //CGRect frame = CGRectMake(0, 0, [self.title sizeWithFont:[UIFont boldSystemFontOfSize:20.0]].width, 44);
   CGRect frame = CGRectMake(0, 0, 180, 44);
    UILabel *label = [[[UILabel alloc] initWithFrame:frame] autorelease];



    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 yellowColor];
    self.navigationItem.titleView = label;
    label.text = NSLocalizedString(@"Latest Questions", @"");
    [label sizeToFit];
}

return self;

這是缺少的事情之一。 您最好的選擇是創建自己的自定義導航欄,添加文本框,並以這種方式操作顏色。

可以在appdelegate文件中使用此方法並可以在每個視圖中使用

+(UILabel *) navigationTitleLable:(NSString *)title
{
CGRect frame = CGRectMake(0, 0, 165, 44);
UILabel *label = [[[UILabel alloc] initWithFrame:frame] autorelease];
label.backgroundColor = [UIColor clearColor];
label.font = NAVIGATION_TITLE_LABLE_SIZE;
label.shadowColor = [UIColor whiteColor];
label.numberOfLines = 2;
label.lineBreakMode = UILineBreakModeTailTruncation;    
label.textAlignment = UITextAlignmentCenter;
[label setShadowOffset:CGSizeMake(0,1)]; 
label.textColor = [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1.0];

//label.text = NSLocalizedString(title, @"");

return label;
}

titleTextAttributes顯示欄標題文本的屬性。

@property(非原子,復制)NSDictionary * titleTextAttributes討論您可以使用NSString UIKit Additions Reference中描述的文本屬性鍵為文本屬性字典中的標題指定字體,文本顏色,文本陰影顏色和文本陰影偏移。

可用性適用於iOS 5.0及更高版本。 在UINavigationBar.h中聲明

你應該調用[label sizeToFit]; 設置文本以防止在其他按鈕占據導航欄時標題視圖中自動重新定位標簽時的奇怪偏移。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM