【问题标题】:UILabel in UINavigationBar is cut off in landscapeUINavigationBar 中的 UILabel 在横向中被截断
【发布时间】:2019-06-26 14:05:40
【问题描述】:

Swift 5、Xcode 10.2

我想在UINavigationBar 中显示两行文本,这就是我使用UILabel 的原因:

@IBOutlet weak var navigationBar: UINavigationItem!

override func viewDidLoad() {
    let labeltext = NSMutableAttributedString.init(string: "Text in line 1\n(Line 2)")
    labeltext.setAttributes([NSAttributedString.Key.font: UIFont.systemFont(ofSize: 14)],range: NSRange(location: labeltext.length-8, length: 8))
    let label = UILabel()
    label.backgroundColor = .clear
    label.numberOfLines = 2
    label.font = UIFont.boldSystemFont(ofSize: 17.0)
    label.textAlignment = .center
    label.textColor = .black
    label.attributedText = labeltext
    navigationBar.titleView = label
}

纵向显示如下:

在较小的设备上,横向文本被截断(在 iPhone Xs Max 和 iPad 上很好):

我尝试按照here 的建议扩展UINavigationBar:

extension UINavigationBar {
    open override func sizeThatFits(_ size: CGSize) -> CGSize {
        let portraitSize = CGSize(width: UIScreen.main.bounds.width, height: 44)
        return portraitSize
    }
}

...但没有改变任何东西,print("Navigationbar height: \(navigationController!.navigationBar.frame.height)") 总是打印“44”,无论它是什么方向以及我使用什么设备(即使在 iPhone X 上)。

按照here 的建议添加观察者也没有帮助。

如何解决此问题以显示 2 行代码,同时又不会为更大的设备/带有缺口的设备破坏它?

【问题讨论】:

  • 也许你应该在设备改变方向上编写代码......或 UIDevice.current.orientation 的条件
  • 这将如何帮助我让它在横向显示 2 行? navigationController!.navigationBar.frame.height 对两个方向都给出了相同的结果,扩展应该考虑横向的高度,但它没有。

标签: ios swift uilabel uinavigationbar


【解决方案1】:

我没有找到让UINavigationBar 显示两行文本或增加其高度的方法(我在问题中发布的扩展名没有做任何事情)。相反,我现在使用此代码根据设备和设备方向更改标签:

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    if (UIDevice.current.orientation == .landscapeLeft || UIDevice.current.orientation == .landscapeRight) && UIDevice.current.userInterfaceIdiom == .phone {
        //1 line: iPhone & horizontal (the NavigationBar on iPads is tall enought to display 2 lines!)
        navigationBar.titleView = labelLandscape!
    } else {
        //2 lines: Vertical (device doesn't matter)
        navigationBar.titleView = labelPortrait!
    }
}

示例标签:

//let labelTextPortrait = NSMutableAttributedString.init(string: "Text in line 1\n(Line 2)")
let labelTextLandscape = NSMutableAttributedString.init(string: "Text in line 1 (Line 2)")
labelTextLandscape.setAttributes([NSAttributedString.Key.font: UIFont.systemFont(ofSize: 14)],range: NSRange(location: labeltextQuer.length-8, length: 8))
//Text size of "(Line 2)" is 8, the remaining text uses 14

var labelLandscape = UILabel()
labelLandscape!.backgroundColor = .clear
labelLandscape!.numberOfLines = 1 //labelPortrait!.numberOfLines = 2
labelLandscape!.font = UIFont.boldSystemFont(ofSize: 17.0)
labelLandscape!.textAlignment = .center
labelLandscape!.textColor = .black
labelLandscape!.attributedText = labelTextLandscape
labelLandscape!.lineBreakMode = .byWordWrapping //important, so it doesn't truncate mid-text!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-20
    • 2017-03-04
    • 2016-01-09
    • 2021-08-30
    相关资源
    最近更新 更多