【问题标题】:Uppercase string for all UINavigation bar titles所有 UI 导航栏标题的大写字符串
【发布时间】:2014-12-30 22:11:12
【问题描述】:

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

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

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

【问题讨论】:

  • 似乎不可能,尽管这是相关的:stackoverflow.com/a/6727489

标签: ios objective-c uiappearance


【解决方案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的设置了。

    【讨论】:

      猜你喜欢
      • 2012-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-14
      • 2020-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多