Tôi có thể thay đổi phông chữ của UINavigationController của mình không? ->titleThay đổi phông chữ của tiêu đề UINavigationController
Trả lời
Chế độ xem tiêu đề có thể là bất kỳ chế độ xem nào. Vì vậy, chỉ cần tạo một UILabel hoặc một cái gì đó khác mà bạn thay đổi phông chữ và gán cái nhìn mới đó vào thuộc tính tiêu đề của mục điều hướng.
Một ví dụ:
-(void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
CGRect frame = CGRectMake(0, 0, 400, 44);
UILabel *label = [[[UILabel alloc] initWithFrame:frame] autorelease];
label.backgroundColor = [UIColor clearColor];
label.font = [FontHelper fontFor:FontTargetForNavigationHeadings];
label.textAlignment = UITextAlignmentCenter;
label.textColor = [UIColor whiteColor];
label.text = self.navigationItem.title;
// emboss in the same way as the native title
[label setShadowColor:[UIColor darkGrayColor]];
[label setShadowOffset:CGSizeMake(0, -0.5)];
self.navigationItem.titleView = label;
}
+1 cho việc này. Nếu viewControllers trong stack yêu cầu phông chữ khác nhau cho tiêu đề, thì '[UINavigationBar appearance]' không thể thực hiện công việc. Tiêu đề tùy chỉnh là phương pháp duy nhất. – BabyPanda
của nó tốt hơn để đặt mã này trong viewDidLoad, bất cứ khi nào khung nhìn xuất hiện trong hệ thống phân cấp, titleView sẽ được tạo và công việc không cần thiết của nó trên hệ thống – raw3d
Tính đến iOS 5 bạn có thể thay đổi font qua proxy xuất hiện.
https://developer.apple.com/documentation/uikit/uiappearance
Sau đây sẽ thiết lập font tiêu đề cho tất cả UINavigationControllers.
NSMutableDictionary *titleBarAttributes = [NSMutableDictionary dictionaryWithDictionary: [[UINavigationBar appearance] titleTextAttributes]];
[titleBarAttributes setValue:[UIFont fontWithName:@"Didot" size:16] forKey:NSFontAttributeName];
[[UINavigationBar appearance] setTitleTextAttributes:titleBarAttributes];
Để thiết lập phông chữ cho các nút quay lại, làm như sau:
NSMutableDictionary *attributes = [NSMutableDictionary dictionaryWithDictionary: [[UIBarButtonItem appearance] titleTextAttributesForState:UIControlStateNormal]];
[attributes setValue:[UIFont fontWithName:@"Didot" size:12] forKey:NSFontAttributeName];
[[UIBarButtonItem appearance] setTitleTextAttributes:attributes forState:UIControlStateNormal];
iOS 7 không dùng khóa: UITextAttributeFont (đọc, không sử dụng) và bây giờ sử dụng khóa: NSFontAttributeName thay thế. Công trình kỳ diệu và làm cho ứng dụng của tôi trông đẹp hơn rất nhiều. Cảm ơn! –
Trong mỗi trình điều khiển chế độ xem riêng lẻ, bạn cũng có thể đặt thuộc tính văn bản tiêu đề trên thể hiện của thanh điều hướng hiện tại để tùy chỉnh giao diện ở đó, như đoạn mã này: [self.navigationController.navigationBar setTitleTextAttributes: @ {NSFontAttributeName: [UIFont fontWithName: @ " Palatino-Bold "size: 28], NSForegroundColorAttributeName: [UIColor whiteColor]}]; – CodePlumber
cho iOS8 + bạn có thể sử dụng:
[self.navigationController.navigationBar setTitleTextAttributes:@{ NSFontAttributeName: [UIFont fontWithName:@"MyFont" size:18.0f],
NSForegroundColorAttributeName: [UIColor whiteColor]
}];
Cảm ơn điều này đã cho tôi hướng đi đúng để thực hiện một phông chữ tùy chỉnh nhanh chóng: self.navigationController? .navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont (tên: "MyFont", kích thước: 18.0)!] – NBoymanns
Cảm ơn, bạn đời! Giải quyết nó. – Felipe
Luôn buồn khi thử trước hết các câu trả lời ở trên chỉ để tìm ra câu trả lời cuối cùng là câu trả lời hiện đại nhất \: – hashier
Câu trả lời từ @morgancodes sẽ thiết lập phông chữ cho tất cả UINavigationController
tiêu đề. Tôi đã cập nhật nó cho Swift 4:
let attributes = [NSAttributedStringKey.font: UIFont(name: "Menlo", size: 14) as Any]
UINavigationBar.appearance().titleTextAttributes = attributes
UIBarButtonItem.appearance().setTitleTextAttributes(attributes, for: .normal)
Đó là một ý tưởng tuyệt vời, nhưng tôi có một vấn đề bây giờ, .. Khi tôi thêm tôi UILabel điều khiển của tôi (như AddButton) trên UINavigationController là không nhìn thấy được nữa , .. * hm * –
Tôi đã cố định nó, bởi chính tôi .. xin lỗi một điều sai lầm;) –
nếu điều này khắc phục được sự cố, đừng quên chấp nhận giải pháp được cung cấp trong câu trả lời – Erik