2012-03-08 43 views
13

Tôi cần thay đổi kích thước của văn bản tiêu đề của Thanh điều hướng cho một bộ điều khiển chế độ xem trong ứng dụng iPhone của tôi. Tôi đang sử dụng iOS5 và đã thử mã sau:Kích thước phông chữ tiêu đề trên thanh điều hướng

if ([self.tabBarItem respondsToSelector:@selector(setTitleTextAttributes:)]) { 
    NSLog(@"*** Support method(iOS 5): setTitleTextAttributes:"); 
    [self.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: 
               [UIFont fontWithName:@"AmericanTypewriter" size:20.0f], UITextAttributeFont, 
               [UIColor blackColor], UITextAttributeTextColor, 
               [UIColor grayColor], UITextAttributeTextShadowColor, 
               [NSValue valueWithUIOffset:UIOffsetMake(0.0f, 1.0f)], UITextAttributeTextShadowOffset, 
               nil]]; 
} 

Tuy nhiên điều này chỉ áp dụng cho tabBarItem.

Trả lời

13

Bạn cần lấy thuộc tính thanh điều hướng và sử dụng @property(nonatomic, copy) NSDictionary *titleTextAttributes.

Để biết thêm thông tin, hãy xem UINavigationBar Class ReferenceCustomizing UINavigationBar section.

Để hiểu rõ hơn câu hỏi của bạn: Bạn có loại bộ điều khiển nào?

EDIT

[self.navigationController.navigationBar setTitleTextAttributes:...]; 

nếu bạn truy cập vào navigationController trong một bộ điều khiển đẩy.

[navigationController.navigationBar setTitleTextAttributes:...]; 

nếu navigationControllerUINavigationController hiện tại của bạn. Điều này có thể được sử dụng khi ví dụ: nếu bạn có mã sau:

UINavigationController* navigationController = ...; 
[navigationController.navigationBar setTitleTextAttributes:...]; 
+1

gì nếu bạn chỉ muốn xem đẩy để có một phông chữ khác nhau? Tôi muốn chế độ xem gốc của tôi có kích thước phông chữ chuẩn, nhưng chế độ xem được đẩy để có phông chữ nhỏ hơn. Tôi đã có nó để làm việc với thiết lập phông chữ nhỏ hơn, nhưng khi tôi trở lại xem gốc, font vẫn còn nhỏ khi tôi muốn nó là kích thước tiêu chuẩn. –

25

Bạn đã có nó, nhưng bạn cũng có thể sử dụng các phương pháp thuộc tính sau đây.

Đối với màu,

NSDictionary *attributes=[NSDictionary dictionaryWithObjectsAndKeys:[UIColor RedColor],UITextAttributeTextColor, nil]; 

self.navigationController.navigationBar.titleTextAttributes = attributes; 

Đối với Size,

NSDictionary *size = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"Arial" size:17.0],UITextAttributeFont, nil]; 

self.navigationController.navigationBar.titleTextAttributes = size; 

Cảm ơn.

Đối với iOS 7 trở lên

Đối với Kích thước:

NSDictionary *size = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"Arial" size:17.0],NSFontAttributeName, nil]; 

self.navigationController.navigationBar.titleTextAttributes = size; 
+1

kích thước vẫn giữ nguyên cho tôi ... –

+0

hiển thị cho tôi mã ur –

+0

Tôi phát hiện ra nó không hoạt động khi phông chữ không được nhập đúng cách –

10

Kể từ iOS7, tôi luôn luôn làm như sau:

[[UINavigationBar appearance] setTitleTextAttributes: 
    [NSDictionary dictionaryWithObjectsAndKeys: 
     [UIFont fontWithName:@"Helvetica-Bold" size:18.0],NSFontAttributeName, 
     nil]]; 

Các từ khóa UITextAttributeFont đã chán nản từ iOS7, bạn phải thay thế bằng NSFontAttributeName.

Bạn có thể sử dụng [self.navigationController.navigationBar setTitleTextAttributes:] để thiết lập sự xuất hiện một ai đó navigationController, trong khi [UINavigationBar appearance] setTitleTextAttributes:] công trình cho toàn bộ ứng dụng của bạn trực tiếp. (Tất nhiên bạn cần phải đặt nó ở một vị trí bất động sản.)

2

Swift 2.0:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { 
     UINavigationBar.appearance().titleTextAttributes = [ 
      NSFontAttributeName: UIFont(name: "DINNextLTW04-Regular", size: 20)! 
     ] 

     return true 
    } 

Hoặc

override func viewDidLoad() { 
    super.viewDidLoad() 

    self.navigationController?.navigationBarHidden = false 
    self.title = "SAMPLE" 

//Set Color 
    let attributes: AnyObject = [ NSForegroundColorAttributeName: UIColor.redColor()] 
    self.navigationController!.navigationBar.titleTextAttributes = attributes as? [String : AnyObject] 


//Set Font Size 
    self.navigationController!.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "Arial", size: 37.0)!]; 

} 
0

dụ My

guard let sansLightFont = UIFont(name: "OpenSans", size: 20) else { return } 
navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName : sansLightFont] 
Các vấn đề liên quan