Можно ли изменить шрифт моего UINavigationController? → title
Изменить шрифт названия UINavigationController
Ответ 1
Вид заголовка может быть любым видом. Поэтому просто создайте UILabel или что-то еще, где вы измените шрифт и назначьте это новое представление для свойства title элемента навигации.
Ответ 2
Начиная с iOS 5 вы можете изменить шрифт через внешний прокси.
https://developer.apple.com/documentation/uikit/uiappearance
Далее будет установлен шрифт заголовка для всех контроллеров UINavigationControllers.
NSMutableDictionary *titleBarAttributes = [NSMutableDictionary dictionaryWithDictionary: [[UINavigationBar appearance] titleTextAttributes]];
[titleBarAttributes setValue:[UIFont fontWithName:@"Didot" size:16] forKey:NSFontAttributeName];
[[UINavigationBar appearance] setTitleTextAttributes:titleBarAttributes];
Чтобы установить шрифт для кнопки "Назад", сделайте следующее:
NSMutableDictionary *attributes = [NSMutableDictionary dictionaryWithDictionary: [[UIBarButtonItem appearance] titleTextAttributesForState:UIControlStateNormal]];
[attributes setValue:[UIFont fontWithName:@"Didot" size:12] forKey:NSFontAttributeName];
[[UIBarButtonItem appearance] setTitleTextAttributes:attributes forState:UIControlStateNormal];
Чтобы установить шрифт для больших заголовков, доступных в iOS 11+, сделайте следующее:
if (@available(iOS 11.0, *)) {
NSMutableDictionary *largeTitleTextAttributes = [NSMutableDictionary dictionaryWithDictionary: [[UINavigationBar appearance] largeTitleTextAttributes]];
[largeTitleTextAttributes setValue:[UIFont fontWithName:@"Didot" size:32] forKey:NSFontAttributeName];
[[UINavigationBar appearance] setLargeTitleTextAttributes:largeTitleTextAttributes];
}
Ответ 3
для iOS8+ вы можете использовать:
[self.navigationController.navigationBar setTitleTextAttributes:@{ NSFontAttributeName: [UIFont fontWithName:@"MyFont" size:18.0f],
NSForegroundColorAttributeName: [UIColor whiteColor]
}];
Swift:
self.navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "MyFont", size: 18.0)!]
Ответ 4
Пример:
-(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;
}
Ответ 5
Ответ от @morgancodes установит шрифт для всех UINavigationController
титров. Я обновил его для Swift 4:
let attributes = [NSAttributedStringKey.font: UIFont(name: "Menlo", size: 14) as Any]
UINavigationBar.appearance().titleTextAttributes = attributes
UIBarButtonItem.appearance().setTitleTextAttributes(attributes, for: .normal)