开发者

Changing NavigationItem’s font

I have used this code 开发者_JS百科to change a NavigationItem’s title:

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

How can I change the font size and font as well?


Sample Code :

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;


As of iOS5+, you can use the UIAppearance protocol to change the font of the nav title, here's code:

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


Since iOS5 you can use the new titleTextAttributes property like so:

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

Possible keys are:

UITextAttributeFont
UITextAttributeTextColor
UITextAttributeTextShadowColor
UITextAttributeTextShadowOffset


To make the above example centered you have to have the size be only as wide as the text, this should do the trick

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


As of iOS5, you can do this in 1 line:

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


Swift 4

in AppDelegate.swift

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


Here is the modern Swift 2 solution:

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


//Appearance setup for navigation title font throught application...

let customFont = UIFont(name: "Helvetica-Bold", size: 15)
UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.font : customFont!]


I have found some good answer for this .I think it's similar to the accepted answer however please find it in some more detail.

- (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;

} 

For more details, Please refer this Link


For swift 3

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜