2013-05-14 30 views
43

Tôi muốn có thể đặt phông chữ của thanh điều hướng ứng dụng trở lại mà không làm bất kỳ điều gì quá điên rồ và không mất bất kỳ đặc điểm thiết kế nào khác của nút (tức là tôi muốn giữ mũi tên) .Thay đổi phông chữ của nút thanh điều hướng trở lại

Ngay bây giờ tôi sử dụng điều này trong viewDidAppear: để đặt phông chữ của các mục nút thanh bình thường.

for (NSObject *view in self.navigationController.navigationBar.subviews) { 
    if ([view isKindOfClass:[UIButton class]]) { 
     [((UIButton*)view).titleLabel setFont:[UIFont 
           fontWithName:@"Gill Sans" 
             size:14.0]]; 
    } 
} 

Tuy nhiên điều này không làm thay đổi nút quay lại, bất kể mã nào được áp dụng cho (gốc, hiện tại, v.v.).

+0

Tuỳ chỉnh văn thể các hạng mục UIBarButton: http://stackoverflow.com/a/28347428/469614 – Vexy

Trả lời

108

Để thay đổi sự xuất hiện của văn bản trong tất cả UIBarButtonItems xuất hiện trong tất cả các UINavigationBars, làm như sau trong application:didFinishLaunchingWithOptions:

[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] setTitleTextAttributes: 
    @{UITextAttributeTextColor:[UIColor blackColor], 
    UITextAttributeTextShadowOffset:[NSValue valueWithUIOffset:UIOffsetMake(0, 1)], 
    UITextAttributeTextShadowColor:[UIColor whiteColor], 
    UITextAttributeFont:[UIFont boldSystemFontOfSize:12.0] 
    } 
    forState:UIControlStateNormal]; 

UPDATE: iOS7 phiên bản thân thiện với

NSShadow *shadow = [[NSShadow alloc] init]; 
shadow.shadowOffset = CGSizeMake(0.0, 1.0); 
shadow.shadowColor = [UIColor whiteColor]; 

[[UIBarButtonItem appearanceWhenContainedIn:[UINavigationBar class], nil] 
setTitleTextAttributes: 
@{NSForegroundColorAttributeName:[UIColor blackColor], 
    NSShadowAttributeName:shadow, 
    NSFontAttributeName:[UIFont boldSystemFontOfSize:12.0] 
    } 
forState:UIControlStateNormal]; 

Swift:

Chú ý: điều này thay đổi tất cả các trường hợp của UIBarButtonItem, không chỉ những chứa trong một UINavigationBar

UIBarButtonItem.appearance() 
       .setTitleTextAttributes([NSFontAttributeName : ExamplesDefaults.fontWithSize(22)], 
             forState: UIControlState.Normal) 

Swift3:. UIBarButtonItem.appearance() setTitleTextAttributes ([NSFontAttributeName: UIFont (tên : "FontName-Regular", kích thước: 14.0)!], Cho: .normal)

+1

Điều này chắc chắn là một cách đơn giản hơn để làm điều đó và nó áp dụng cho tất cả các mục nút thanh trong tương lai, không chỉ là nút quay lại. – zachjs

+1

Hoạt động tốt. Lưu ý UITextAttributeTextColor, vv không được chấp nhận trong iOS 7. – LordParsley

+0

Cảm ơn bạn !. Nó làm việc cho tôi. – Raja

1

Sử dụng này để thay thế trong appdelegate của bạn hoặc nơi NavigationController được khởi tạo, phương pháp có sẵn trong iOS 5 trở lên

UIBarButtonItem *backbutton = [[UIBarButtonItem alloc] initWithTitle:@"back" style:UIBarButtonItemStyleBordered target:nil action:nil]; 
[backbutton setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: 
                [UIColor blackColor],UITextAttributeTextColor,[UIFont fontWithName:TEXTFONT size:16.0f],UITextAttributeFont, 
                nil] forState:UIControlStateNormal]; 
+1

Đây không phải là thực sự là một câu trả lời hoàn chỉnh vì backButton được cấu hình không được sử dụng. –

+0

@MikePollard câu hỏi cho biết thay đổi phông chữ không phải cách sử dụng nút. nếu bạn muốn biết cách sử dụng nó, bạn nên hỏi. –

6

Đối với những người không hoàn toàn làm việc này, dưới đây là cách tôi đã làm, bao gồm cả popped quay lại Root ViewController trong IOS7 :

UIBarButtonItem *backBtn =[[UIBarButtonItem alloc]initWithTitle:@"Back" style:UIBarButtonItemStyleDone target:self action:@selector(popToRoot:)]; 
backBtn.title = @"Back"; 
[backBtn setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: 
           [UIFont fontWithName:@"Chalkduster" size:15], NSFontAttributeName, 
           [UIColor yellowColor], NSForegroundColorAttributeName, 
           nil] 
         forState:UIControlStateNormal]; 

self.navigationItem.leftBarButtonItem=backBtn; 

popToRoot ViewController:

- (IBAction)popToRoot:(UIBarButtonItem*)sender { 
[self.navigationController popToRootViewControllerAnimated:YES]; 
} 

Có lẽ ai đó có thể có sử dụng điều này.

+0

Sẽ không hoạt động với các ngôn ngữ và ném đi các gợi ý truy cập được định cấu hình trước cần thiết để đưa ứng dụng vào IAS. Bạn nên luôn sử dụng các nút được cung cấp bởi hệ thống trừ khi bạn muốn thực hiện nhiều công việc phụ. – Barry

0

Nếu bạn đang sử dụng UISplitViewControllerDelegate mới cho chế độ xem chia nhỏ trong iOS 8, các phương pháp trên sẽ không hoạt động vì displayModeButtonItem mới hoạt động hơi khác một chút.

Bạn cần đặt phông chữ khi đang tạo displayModeButtonItem.Giả sử bạn đang theo dõi mẫu của Apple này có lẽ là trong prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender nơi bạn sẽ làm điều gì đó như thế này:

// From Apple's Template: 
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow]; 
NSManagedObject *object = [[self fetchedResultsController] objectAtIndexPath:indexPath]; 
DetailViewController *controller = (DetailViewController *)[[segue destinationViewController] topViewController]; 
[controller setDetailItem:object]; 
controller.navigationItem.leftBarButtonItem = self.splitViewController.displayModeButtonItem; 
controller.navigationItem.leftItemsSupplementBackButton = YES; 
// New Line to set the font: 
[controller.navigationItem.leftBarButtonItem setTitleTextAttributes:@{NSFontAttributeName : [UIFont fontWithName:@"SourceSansPro-Regular" size:14]} forState:UIControlStateNormal]; 
+0

Trên thực tế sau khi thử nghiệm nhiều hơn, tôi không nghĩ rằng điều này hoạt động. Nếu ai đó có ý tưởng về cách thay đổi phông chữ trên 'displayModeButtonItem' hãy cho tôi biết. – Nick

4

phiên bản Swift của tất cả các nêu trên (trích đoạn từ original answer):

let customFont = UIFont(name: "customFontName", size: 17.0)! 
UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: customFont], forState: .normal) 

More goodies :
https://stackoverflow.com/a/28347428/469614

2

Trong Swift3:

let font = UIFont(name: "Verdana", size: 10.0) 

// Back button 
UIBarButtonItem.appearance().setTitleTextAttributes([NSFontAttributeName: font!], for: UIControlState.normal) 

// Title in the navigation item 
let fontAttributes = [NSFontAttributeName: font] 
self.navigationController?.navigationBar.titleTextAttributes = fontAttributes 

Lưu ý: bạn chỉ phải thực hiện thao tác này một lần cho bộ điều khiển Điều hướng.

1

Swift 3.0+

AppDelegate.swift

UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: "myFont", size: 17.0)!], for: .normal)