Ios 所有UINavigation栏标题的大写字符串

Ios 所有UINavigation栏标题的大写字符串,ios,objective-c,uiappearance,Ios,Objective C,Uiappearance,目前,我正在使用AppDelegate中的以下命令更改导航栏的字体: [[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys: [UIFont fontWithName:@"..." size:...], NSFontAttributeName, nil]]; 是否有办法确保字符串全局大写?您可以创建一个基本的UIViewController,它隐

目前,我正在使用
AppDelegate
中的以下命令更改导航栏的字体:

[[UINavigationBar appearance] setTitleTextAttributes:
 [NSDictionary dictionaryWithObjectsAndKeys:
  [UIFont fontWithName:@"..." size:...], NSFontAttributeName,
  nil]];

是否有办法确保字符串全局大写?

您可以创建一个基本的
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];

然后,您可以控制标题标签的标题设置。

使用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()
    }
}

您可以将
UINavigationBar
外观所使用的
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)
}

}

似乎不可能,但这是相关的:stackoverflow.com/a/6727489这些都是小盘股。有机会用普通的帽子吗?