所以我有这个代码应该更改导航栏标题字体,但它确实
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont
fontWithName:_dataManager.optionsSettings.fontString size:14], NSFontAttributeName,
[UIColor whiteColor], NSForegroundColorAttributeName, nil];
[[UINavigationBar appearance] setTitleTextAttributes:attributes];
使用此代码更改后退按钮字体可以正常工作。
//set backbutton font
NSDictionary *normalAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:_dataManager.optionsSettings.fontString size:15], NSFontAttributeName,
nil];
[[UIBarButtonItem appearance] setTitleTextAttributes:normalAttributes
forState:UIControlStateNormal];
答案 0 :(得分:223)
更改标题字体(和颜色)的正确方法是:
[self.navigationController.navigationBar setTitleTextAttributes:
@{NSForegroundColorAttributeName:[UIColor redColor],
NSFontAttributeName:[UIFont fontWithName:@"mplus-1c-regular" size:21]}];
编辑:Swift 4.2
self.navigationController?.navigationBar.titleTextAttributes =
[NSAttributedString.Key.foregroundColor: UIColor.red,
NSAttributedString.Key.font: UIFont(name: "mplus-1c-regular", size: 21)!]
编辑:Swift 4
self.navigationController?.navigationBar.titleTextAttributes =
[NSAttributedStringKey.foregroundColor: UIColor.red,
NSAttributedStringKey.font: UIFont(name: "mplus-1c-regular", size: 21)!]
斯威夫特3:
self.navigationController?.navigationBar.titleTextAttributes =
[NSForegroundColorAttributeName: UIColor.redColor(),
NSFontAttributeName: UIFont(name: "mplus-1c-regular", size: 21)!]
答案 1 :(得分:50)
其他答案没有错。我只是分享故事板版本来设置字体。
(在Xcode选择新字体之前,您可能需要切换导航栏的条形色调)
已验证这适用于Xcode 7.1.1+。 (请参阅下面的示例)
其中一些重复,这意味着它们很可能值得注意。
注意〜可以从Code With Chris网站找到nice checklist,您可以看到示例下载项目。
如果您有自己的字体并希望在故事板中使用它,那么下面的SO Question会有一套不错的答案。一个答案确定了这些步骤。
所以Xcode自然看起来它可以处理UINavigationItem上的自定义字体,但该功能没有正确更新(忽略所选字体)。
要解决此问题:
一种方法是使用故事板修复并添加一行 代码:首先添加一个UIView(UIButton,UILabel或其他一些UIView 子类)到视图控制器(不是导航项...... Xcode当前不允许这样做)。添加控件后 您可以修改故事板中的字体并添加引用作为 视频控制器的插座。只需将该视图分配给 UINavigationItem.titleView。您还可以在代码中设置文本名称 如有必要。报告错误(23600285)。
@IBOutlet var customFontTitleView: UIButton!
//Sometime later...
self.navigationItem.titleView = customFontTitleView
答案 2 :(得分:21)
试试这个:
NSDictionary *textAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor],NSForegroundColorAttributeName,
[UIColor whiteColor],NSBackgroundColorAttributeName,nil];
self.navigationController.navigationBar.titleTextAttributes = textAttributes;
答案 3 :(得分:11)
我的Swift代码更改导航栏标题:
let attributes = [NSFontAttributeName : UIFont(name: "Roboto-Medium", size: 16)!, NSForegroundColorAttributeName : UIColor.whiteColor()]
self.navigationController.navigationBar.titleTextAttributes = attributes
如果你想改变背景字体,那么我在AppDelegate中有这个:
let attributes = [NSFontAttributeName : UIFont(name: "Roboto-Medium", size: 16)!, NSForegroundColorAttributeName : UIColor.whiteColor()]
UIBarButtonItem.appearance().setTitleTextAttributes(attributes, forState: UIControlState.Normal)
答案 4 :(得分:4)
以下是您的问题的答案:
将代码移动到下面的方法,因为导航栏标题在加载视图后更新。我尝试在viewDidLoad中添加上面的代码并不起作用,它在viewDidAppear方法中工作正常。
-(void)viewDidAppear:(BOOL)animated{}
答案 5 :(得分:3)
任何人都需要Swift 3版本。 redColor()
已更改为red
。
self.navigationController?.navigationBar.titleTextAttributes =
[NSForegroundColorAttributeName: UIColor.red,
NSFontAttributeName: UIFont(name: "{your-font-name}", size: 21)!]
答案 6 :(得分:2)
使用文字更具可读性:
self.navigationController.navigationBar.titleTextAttributes = @{
NSFontAttributeName:[UIFont fontWithName:@"mplus-1c-regular" size:21],
NSForegroundColorAttributeName: [UIColor whiteColor]
};
答案 7 :(得分:2)
迅速:-
self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white, NSAttributedStringKey.font: UIFont(name:"Love Nature", size: 40)!]
答案 8 :(得分:2)
在您的应用程序委托中添加此单行代码-是否完成Lauch。它将更改整个应用程序中导航栏的字体,颜色。
UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white, NSAttributedString.Key.font: UIFont(name: "YOUR FONT NAME", size: 25.0)!]
答案 9 :(得分:1)
这个替代Swift 4(已经由@Josh回答):
let titleTextAttributed: [NSAttributedStringKey: Any] = [.foregroundColor: UIColor.red, .font: UIFont(name: "AvenirNext-Regular", size: 20) as Any]
navigationController?.navigationBar.titleTextAttributes = titleTextAttributed
答案 10 :(得分:1)
if (@available(iOS 11.0, *)) {
self.navigationController.navigationItem.largeTitleDisplayMode = UINavigationItemLargeTitleDisplayModeAlways;
self.navigationController.navigationBar.prefersLargeTitles = true;
// Change Color
self.navigationController.navigationBar.largeTitleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor]};
} else {
// Fallback on earlier versions
}
答案 11 :(得分:1)
要让Objective-C设置字体和颜色
- (void)_setup {
NSDictionary *barButtonTitleAttributes = @{
NSForegroundColorAttributeName : [UIColor whiteColor],
NSFontAttributeName :[UIFont fontWithName:@"Lato-Regular" size:15.0]
};
[self.navigationBar setTitleTextAttributes:barButtonTitleAttributes];
}
答案 12 :(得分:0)
以下是Swift 4的答案:
let textAttributes:[String:Any]? = [NSAttributedStringKey.foregroundColor.rawValue:UIColor.blue, NSAttributedStringKey.font.rawValue:UIFont(name:"Avenir Next", size:20)!]
navigationController?.navigationBar.titleTextAttributes = textAttributes
答案 13 :(得分:0)
不要忘记添加密钥的Raw值以避免编译错误。
let textAttributes:[NSAttributedStringKey: Any] = [NSAttributedStringKey(rawValue: NSAttributedStringKey.foregroundColor.rawValue):UIColor.blue, NSAttributedStringKey(rawValue: NSAttributedStringKey.font.rawValue):UIFont(name:"OpenSans", size: 17)!]
navigationController?.navigationBar.titleTextAttributes = textAttributes
答案 14 :(得分:0)
Swift 4.2
self.navigationController?.navigationBar.titleTextAttributes =
[NSAttributedString.Key.foregroundColor: UIColor.white,
NSAttributedString.Key.font: UIFont(name: "LemonMilklight", size: 21)!]
答案 15 :(得分:0)
添加此扩展名
extension UINavigationBar {
func changeFont() {
self.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white, NSAttributedStringKey.font: UIFont(name:"Poppins-Medium", size: 17)!]
}
}
在viewDidLoad()中添加以下行
self.navigationController?.navigationBar.changeFont()
答案 16 :(得分:-1)
在swift 3.0中工作 要更改标题颜色,您需要添加titleTextAttributes,如此
let textAttributes = [NSForegroundColorAttributeName:UIColor.white]
self.navigationController.navigationBar.titleTextAttributes = textAttributes
For changing navigationBar background color you can use this
self.navigationController.navigationBar.barTintColor = UIColor.white
For changing navigationBar back title and back arrow color you can use this
self.navigationController.navigationBar.tintColor = UIColor.white