[英]UILabel in UINavigationBar is cut off in landscape
Swift 5, Xcode 10.2 Swift 5,Xcode 10.2
I want to display 2 lines of text in the UINavigationBar
, that's why I'm using a UILabel
: 我想在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
}
In portrait orientation it looks like this: 纵向显示如下:
On smaller devices the text is cut off in landscape (it's fine on iPhone Xs Max and iPad): 在较小的设备上,文字会横向剪切(在iPhone Xs Max和iPad上很好):
I tried to extend UINavigationBar
as suggested here : 我试图按照此处的建议扩展UINavigationBar
:
extension UINavigationBar {
open override func sizeThatFits(_ size: CGSize) -> CGSize {
let portraitSize = CGSize(width: UIScreen.main.bounds.width, height: 44)
return portraitSize
}
}
... but didn't change anything and print("Navigationbar height: \\(navigationController!.navigationBar.frame.height)")
always prints "44", no matter what the orientation it is and what device I use (even on iPhone X). ...但没有进行任何更改并print("Navigationbar height: \\(navigationController!.navigationBar.frame.height)")
始终显示“ 44”,无论它的方向和使用的设备是什么(即使在iPhone X)。
Adding an observer as suggested here also didn't help. 按照此处的建议添加观察者也无济于事。
How do I fix this to display 2 lines of code while not breaking it for bigger devices/devices with a notch? 我如何解决此问题以显示2行代码,同时又不破坏较大的设备/带有槽口的设备呢?
I didn't find a way to make the UINavigationBar
display two lines of text or increase its height (the extension I posted in the question didn't do anything). 我没有找到使UINavigationBar
显示两行文本或增加其高度的方法(我在问题中发布的扩展名没有任何作用)。 Instead I'm now using this code to change the label depending on the device and device orientation: 相反,我现在使用此代码根据设备和设备方向更改标签:
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!
}
}
Example label: 标签示例:
//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!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.