简体   繁体   English

调整导航栏标题字体大小以适合文本

[英]Adjust navigation bar title font size to fit text

是否可以调整导航栏标题的字体大小以适应文本?

You can create a UILabel and set it to UINavigationItem's titleView.您可以创建一个 UILabel 并将其设置为 UINavigationItem 的 titleView。 See Apple doc: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UINavigationItem_Class/#//apple_ref/occ/instp/UINavigationItem/titleView请参阅 Apple 文档: https : //developer.apple.com/library/ios/documentation/UIKit/Reference/UINavigationItem_Class/#//apple_ref/occ/instp/UINavigationItem/titleView

For the created UILabel, you can set the adjustsFontSizeToFitWidth & minimumScaleFactor properties to let it fit the title.对于创建的 UILabel,您可以设置 adjustsFontSizeToFitWidth 和 minimumScaleFactor 属性使其适合标题。 Doc: https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UILabel_Class/#//apple_ref/occ/instp/UILabel/adjustsFontSizeToFitWidth文档: https : //developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UILabel_Class/#//apple_ref/occ/instp/UILabel/adjustsFontSizeToFitWidth

Some codes:一些代码:

- (void)setMyTitle:(NSString *)title
{
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.navigationController.view.bounds.size.width - 100, 44)];
    titleLabel.text = title;
    titleLabel.font = [UIFont systemFontOfSize:16];
    titleLabel.textColor = ...
    ...
    self.navigationItem.titleView = titleLabel;
}

Try the following in viewDidLoad:在 viewDidLoad 中尝试以下操作:

NSDictionary *attributes = @{ NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue-Bold" size:14] };

[self.navigationController.navigationBar setTitleTextAttributes:attributes];

NOTE: The downside to this solution is that you have to know the font size up front and set it manually.注意:此解决方案的缺点是您必须预先知道字体大小并手动设置。 I'm not sure if you can set the navigation bar's title label to automatically change the font size to fit the text.我不确定您是否可以将导航栏的标题标签设置为自动更改字体大小以适合文本。

EDIT:编辑:
Turns out you can set the navigation bar's label to resize the font size dynamically原来你可以设置导航栏的标签来动态调整字体大小

Swift 5斯威夫特 5

private var isFirst = true
override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()
    guard isFirst else {
    return
    }
    isFirst = false
    // Adjust Navigation Title by text
    if let navigationController = navigationController {
    let bar = navigationController.navigationBar
    var minX: CGFloat = 0
    var maxX: CGFloat = bar.bounds.width
    if let lastLeft = navigationItem.leftBarButtonItems?.last, let leftView = lastLeft.view, let buttonBarStackViewFrame = leftView.superview?.frame {
        minX = buttonBarStackViewFrame.maxX
    }
    if let firstRight = navigationItem.rightBarButtonItems?.first, let rightView = firstRight.view, let buttonBarStackViewFrame = rightView.superview?.frame {
        maxX = buttonBarStackViewFrame.minX
    }

    let titleLabel = UILabel(frame: CGRect(x: 0, y: 0, width: maxX - minX, height: bar.bounds.height))
    titleLabel.text = LocStr(.favorite_master_title)
    titleLabel.adjustsFontSizeToFitWidth = true
    titleLabel.minimumScaleFactor = 0.3
    titleLabel.textColor = UIColor.white
    titleLabel.textAlignment = .center
    if UIDevice.current.modelName.contains(PLUS) {
        titleLabel.font = UIFont(name: GENERAL_FONT_NAME, size: PLUS_NAVIGATION_BAR_TITLE_FONT)!
    } else {
        titleLabel.font = UIFont(name: GENERAL_FONT_NAME, size: GENERAL_NAVIGATION_BAR_TITLE_FONT)!
    }
    navigationItem.titleView = titleLabel
    }
}

with a Extension带扩展名

extension UIBarButtonItem {
    var view: UIView? {
        return value(forKey: "view") as? UIView
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM