【问题标题】:Automatically adjust Navigation Item Title font size to fit自动调整导航项标题字体大小以适应
【发布时间】:2020-06-26 06:17:07
【问题描述】:

我正在使用最新版本的XcodeSwift

我有一个Navigation Bar 和一个Navigation Item Title。我使用的是普通的小标题,而不是 iOS 13 的大标题。

在某些屏幕上,Navigation Item Title 太长,因此它会被截断,并以 结尾

有没有办法自动调整字体大小以适应 Navigation Item Title 中的标题?

【问题讨论】:

  • 导航栏可以让你插入UILabel,这意味着你可以自动调整字体大小,我想。
  • 考虑我对这个问题的回答:stackoverflow.com/questions/14956561/… 要使用普通标题,您必须将字体设置为 UIFont.systemFont(ofSize: 17, weight: .semibold)largeTitleTextAttributestitleTextAttributes
  • 你将不得不调整这条线tempLargeTitleLabel.intrinsicContentSize.width > view.frame.width - 30(而不是我用于普通标题195的30)。在 for 循环中,最后在你实际设置 titleTextAttributes 的地方,你还必须将 font-weight 设置为 .semibold

标签: ios swift xcode uinavigationbar


【解决方案1】:

将下面的代码放在 viewDidLoad() 中,这应该会缩小标题中文本的大小而不会截断它。:

    let navbarTitle = UILabel()
    navbarTitle.text = "My nav title"
    navbarTitle.minimumScaleFactor = 0.5
    navbarTitle.adjustsFontSizeToFitWidth = true
    navigationItem.titleView = navbarTitle

希望这对您有所帮助。使用minimumScaleFactor 查找您想要的值。如果您需要将标题加粗:How to make a UILabel bold

另外,这里有一个可能对您有用的链接:Line break mode and number of lines on UILabel

【讨论】:

    猜你喜欢
    • 2016-04-26
    • 2011-05-23
    • 2012-08-10
    • 2016-04-21
    • 1970-01-01
    • 2013-06-04
    • 1970-01-01
    • 1970-01-01
    • 2013-07-26
    相关资源
    最近更新 更多