Подтвердить что ты не робот

Изменение шрифта NavigationItems

Я использовал этот код для изменения названия NavigationItem:

self.navigationItem.title = @"My Name is ABC";

Как изменить размер шрифта и шрифт?

4b9b3361

Ответ 1

Пример кода:

CGRect frame = CGRectMake(0, 0, 400, 44);
UILabel *label = [[[UILabel alloc] initWithFrame:frame] autorelease];
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont boldSystemFontOfSize:8.0];
label.textAlignment = UITextAlignmentCenter;
label.textColor = [UIColor whiteColor];
label.text = @"Sample custom Title With small Fonts ";
self.navigationItem.titleView = label;

Ответ 2

Начиная с iOS5 +, вы можете использовать протокол UIAppearance, чтобы изменить шрифт названия навигатора, здесь код:

NSMutableDictionary *titleBarAttributes = [NSMutableDictionary dictionaryWithDictionary: [[UINavigationBar appearance] titleTextAttributes]];
[titleBarAttributes setValue:[UIFont fontWithName:@"Helvetica-Bold" size:18] forKey:UITextAttributeFont];
[[UINavigationBar appearance] setTitleTextAttributes:titleBarAttributes];

Ответ 3

Так как iOS5 вы можете использовать новое свойство titleTextAttributes следующим образом:

NSMutableDictionary *newAttributes = [[NSMutableDictionary alloc] init]; 
[newAttributes setObject:[UIFont boldSystemFontOfSize:18] forKey:UITextAttributeFont];
[self.navigationController.navigationBar setTitleTextAttributes:newAttributes];

Возможные ключи:

UITextAttributeFont
UITextAttributeTextColor
UITextAttributeTextShadowColor
UITextAttributeTextShadowOffset

Ответ 4

Чтобы сделать приведенный выше пример центрированным, вы должны иметь размер только ширины, чем текст, это должно сделать трюк

UIFont * font = [UIFont fontWithName:@"Helvetica" size:18.0f];
CGRect frame = CGRectMake(0, 0, [@"Lorem Ipsum" sizeWithFont:font].width, 44);

Ответ 5

Как и в iOS5, вы можете сделать это в 1 строке:

[[UINavigationBar appearance] setTitleTextAttributes: @{UITextAttributeFont: [UIFont fontWithName:@"Helvetica" size:20.0f]}];

Ответ 6

Я нашел для этого хороший ответ. Я думаю, что это похоже на принятый ответ, но, пожалуйста, найдите его более подробно.

- (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 = UITextAlignmentCenter;      
    label.textColor = [UIColor yellowColor]; // change this color      
    self.navigationItem.titleView = label;          
    label.text = NSLocalizedString(@"PageThreeTitle", @"");       
    [label sizeToFit];    
 }    
 return self;

} 

Подробнее см. Ссылка

Ответ 7

Вот современное решение Swift 2:

let customFont = UIFont(name: "Helvetica", size: 17)
UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName : customFont!]

Ответ 8

Для быстрого 3

self.navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName :UIfont(named:"test") ]

Ответ 9

Swift 4

в AppDelegate.swift

UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: "Helvetica-Bold", size: 15)!], for: .normal)