所有 UI 导航栏标题的大写字符串
Posted
技术标签:
【中文标题】所有 UI 导航栏标题的大写字符串【英文标题】:Uppercase string for all UINavigation bar titles 【发布时间】:2014-12-30 22:11:12 【问题描述】:目前,我正在使用AppDelegate
中的以下内容更改导航栏的字体:
[[UINavigationBar appearance] setTitleTextAttributes:
[NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:@"..." size:...], NSFontAttributeName,
nil]];
有没有办法确保字符串全局大写?
【问题讨论】:
似乎不可能,尽管这是相关的:***.com/a/6727489 【参考方案1】:使用 NSAttributedString 无法实现这一点,但您可以在基本视图控制器类中更改标题。 试试这个:
/// Base class for all view controllers
class BaseViewController: UIViewController
private var firstWillAppearOccured = false
override func viewWillAppear(_ animated: Bool)
super.viewWillAppear(animated)
if !firstWillAppearOccured
viewWillFirstAppear(animated)
firstWillAppearOccured = true
/// Method is called when `viewWillAppear(_:)` is called for the first time
func viewWillFirstAppear(_ animated: Bool)
title = title?.uppercased() // Note that title is not available in viewDidLoad()
【讨论】:
【参考方案2】:您可以将UINavigationBar
appearance
使用的UIFont
配置为使用大写字母。
extension UIFont
func getAllCapsFont(with size: CGFloat, andWeight weight: Weight) -> UIFont
let fontDescriptor = UIFont.systemFont(ofSize: size, weight: weight).fontDescriptor
let upperCaseFeature: [UIFontDescriptor.FeatureKey: Int] = [
.featureIdentifier: kUpperCaseType,
.typeIdentifier: kUpperCaseSmallCapsSelector
]
let lowerCaseFeature: [UIFontDescriptor.FeatureKey: Int] = [
.featureIdentifier: kLowerCaseType,
.typeIdentifier: kLowerCaseSmallCapsSelector
]
let features = [upperCaseFeature, lowerCaseFeature]
let additions = fontDescriptor.addingAttributes([.featureSettings: features])
return UIFont(descriptor: additions, size: size)
【讨论】:
这些是小型大写字母。有机会使用普通大写吗?【参考方案3】:您可以创建一个基础UIViewController
隐藏原来的导航栏并自己添加一个新的。
self.navigationController.navigationBarHidden = YES;
UINavigationBar *navigationBar = [[UINavigationBar alloc] initWithFrame:frame];
UINavigationItem *naviItem = [[UINavigationItem alloc] init];
self.titleLabel = [UILabel new];
naviItem.titleView = self.titleLabel;
navigationBar.items = [NSArray arrayWithObjects:naviItem, nil];
[self.view addSubview:navigationBar];
然后就可以控制titleLabel的title的设置了。
【讨论】:
以上是关于所有 UI 导航栏标题的大写字符串的主要内容,如果未能解决你的问题,请参考以下文章