我已使用此代码更改NavigationItem
的标题:
self.navigationItem.title = @"My Name is ABC";
如何更改字体大小和字体?
答案 0 :(得分:32)
示例代码:
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;
答案 1 :(得分:18)
从iOS5 +开始,你可以使用UIAppearance protocol更改导航标题的字体,这里是代码:
NSMutableDictionary *titleBarAttributes = [NSMutableDictionary dictionaryWithDictionary: [[UINavigationBar appearance] titleTextAttributes]];
[titleBarAttributes setValue:[UIFont fontWithName:@"Helvetica-Bold" size:18] forKey:UITextAttributeFont];
[[UINavigationBar appearance] setTitleTextAttributes:titleBarAttributes];
答案 2 :(得分:10)
从iOS5开始,您可以使用新的titleTextAttributes
属性,如下所示:
NSMutableDictionary *newAttributes = [[NSMutableDictionary alloc] init];
[newAttributes setObject:[UIFont boldSystemFontOfSize:18] forKey:UITextAttributeFont];
[self.navigationController.navigationBar setTitleTextAttributes:newAttributes];
可能的关键是:
UITextAttributeFont
UITextAttributeTextColor
UITextAttributeTextShadowColor
UITextAttributeTextShadowOffset
答案 3 :(得分:6)
要使上面的示例居中,你必须使文字的大小只与文字一样宽,这应该可以做到这一点
UIFont * font = [UIFont fontWithName:@"Helvetica" size:18.0f];
CGRect frame = CGRectMake(0, 0, [@"Lorem Ipsum" sizeWithFont:font].width, 44);
答案 4 :(得分:4)
从iOS5开始,您可以在一行中执行此操作:
[[UINavigationBar appearance] setTitleTextAttributes: @{UITextAttributeFont: [UIFont fontWithName:@"Helvetica" size:20.0f]}];
答案 5 :(得分:2)
Swift 4
在AppDelegate.swift
中UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: "Helvetica-Bold", size: 15)!], for: .normal)
答案 6 :(得分:1)
这是现代Swift 2解决方案:
let customFont = UIFont(name: "Helvetica", size: 17)
UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName : customFont!]
答案 7 :(得分:0)
我已经找到了一些很好的答案。我认为这与接受的答案类似,但请更详细地找到答案。
- (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;
}
有关详细信息,请参阅此Link
答案 8 :(得分:0)
对于swift 3
self.navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName :UIfont(named:"test") ]
答案 9 :(得分:0)
//通过应用程序设置导航标题字体的外观...
let customFont = UIFont(name: "Helvetica-Bold", size: 15)
UINavigationBar.appearance().titleTextAttributes = [NSAttributedStringKey.font : customFont!]