I am trying to display text in a UILabel vertically after the device changes to landscape from portrait.
I've managed to (forcefully) stack them vertically by changing its frame size and setting numberOfLines = 0.
This sort of produces the result I want, except I get white spaces between each character after rotation, elongating the word greatly.
Please check the link below to get a better idea of what I am experiencing.
ttps:(Double Slash)ibb[dot]co{SLASH}6g8SHt9
Ideally, there would be no unnecessary white spaces after ratation, and it would be nice and compact like it is in Portrait.
Does anyone know how I can achive this? Below is my code:
import UIKit
class ViewController: UIViewController {
var _W = CGFloat()
var _H = CGFloat()
var wordLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
_W = UIApplication.shared.statusBarOrientation.isPortrait ? view.frame.width : view.frame.height
_H = UIApplication.shared.statusBarOrientation.isPortrait ? view.frame.height : view.frame.width
wordLabel = UILabel()
wordLabel.text = "Text"
view.addSubview(wordLabel)
formatLabel(label: wordLabel, fontSize: 64, color: UIColor.magenta, tag: 1)
}
func formatLabel(label: UILabel, fontSize: CGFloat, color: UIColor, tag: Int) {
label.font = UIFont.boldSystemFont(ofSize: fontSize)
label.textColor = color
label.tag = tag
label.textAlignment = .center
label.adjustsFontSizeToFitWidth = true
label.numberOfLines = 0
positionView(label, isPortrait: UIApplication.shared.statusBarOrientation.isPortrait)
}
override func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator) {
positionView(wordLabel, isPortrait: !UIApplication.shared.statusBarOrientation.isPortrait)
}
func positionView(_ view: UIView, isPortrait: Bool) {
if (view.tag == 1) {
if (isPortrait) {
wordLabel.frame = CGRect(x: 0, y: 0, width: _W, height: 80)
wordLabel.center = CGPoint(x: _W/2, y: (_H - _W)/4)
} else {
wordLabel.frame = CGRect(x: 0, y: 0, width: 50, height: _W)
wordLabel.center = CGPoint(x: (_H - _W)/4, y: _W/2)
}
}
}
}