原文鏈接:點擊打開鏈接
文章來源:http://www.aichengxu.com/view/42504
iOS中設置導航欄標題的字體顏色和大小,有需要的朋友可以參考下。
在平時開發項目的時候,難免會遇到修改導航欄字體大小和顏色的需求,一般使用自定義視圖的方法,其實還存在一種方法。
方法一:(自定義視圖的方法,一般人也會採用這樣的方式)
就是在導航向上添加一個titleView,可以使用一個label,再設置label的背景顏色透明,字體什麼的設置就很簡單了。
//自定義標題視圖
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
titleLabel.backgroundColor = [UIColor grayColor];
titleLabel.font = [UIFont boldSystemFontOfSize:20];
titleLabel.textColor = [UIColor greenColor];
titleLabel.textAlignment = NSTextAlignmentCenter;
titleLabel.text = @"新聞";
self.navigationItem.titleView = titleLabel;
方法二:(在默認顯示的標題中直接修改文件的大小和顏色也是可以的)
[self.navigationController.navigationBar setTitleTextAttributes:
@{NSFontAttributeName:[UIFont systemFontOfSize:19],
NSForegroundColorAttributeName:[UIColor redColor]}];
方式二相對於方式一而言更加簡單方便
[self.navigationItem setTitle:@"修改贈品詳情"];
/**
修改導航欄字體樣式
- returns: <#return value description#>
*/
[self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20],NSForegroundColorAttributeName:[UIColor whiteColor]}];
UIBarButtonItem * lbb = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(back)];