英文:
How to rotate the contents in a label?
问题
我正在使用一个仅支持竖屏的应用程序。我需要在横屏模式下显示一个标签,所以我将标签中的文本旋转了。但是标签的位置发生了变化。如何在不改变标签位置的情况下旋转文本?标签的位置不应该改变,应该与第一张截图中的位置相同。输出应该与最后一张图像类似。
override func viewDidLoad() {
super.viewDidLoad()
rotateLabel(radians: CGFloat.pi/2)
}
func rotateLabel(radians: CGFloat){
rotateLabel.transform = CGAffineTransform(rotationAngle: radians)
}
英文:
I am working in an app which has only portrait support. I need to show a label in landscape orientation so I rotated the text in the label. But the position of the label gets changed. How to rotate the text without changing the position of label? The position of the label should not change. It should be the same as in the first screenshot. The output should be like the last image.
override func viewDidLoad() {
super.viewDidLoad()
rotateLabel(radians: CGFloat.pi/2)
}
func rotateLabel(radians: CGFloat){
rotateLabel.transform = CGAffineTransform(rotationAngle: radians)
}
Before rotation:
After rotation:
Expected output:
答案1
得分: 1
你应该首先创建一个带有框架的标签,就像第二张截图中的那样(宽度 > 高度),然后旋转它,使其变成第一张截图中的框架(高度 > 宽度)。
let label = UILabel()
label.text = "花很美"
label.backgroundColor = .yellow
label.textAlignment = .center
label.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(label)
NSLayoutConstraint.activate([
label.centerYAnchor.constraint(equalTo: view.centerYAnchor),
label.centerXAnchor.constraint(equalTo: view.centerXAnchor),
label.heightAnchor.constraint(equalToConstant: 40),
// 请注意此处的 label.width = view.height 的约束
label.widthAnchor.constraint(equalTo: view.heightAnchor)
])
label.transform = CGAffineTransform(rotationAngle: .pi / 2)
如果要考虑安全区域,您可以添加一个容器视图。容器视图的框架类似于第一张截图,而标签仍然具有类似第二张截图的框架。容器的顶部和底部将约束到安全区域。
let label = UILabel()
label.text = "花很美"
label.textAlignment = .center
label.translatesAutoresizingMaskIntoConstraints = false
label.transform = CGAffineTransform(rotationAngle: .pi / 2)
let container = UIView()
container.backgroundColor = .yellow
container.addSubview(label)
container.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(container)
NSLayoutConstraint.activate([
container.centerXAnchor.constraint(equalTo: view.centerXAnchor),
container.widthAnchor.constraint(equalToConstant: 40),
container.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
container.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
label.centerXAnchor.constraint(equalTo: container.centerXAnchor),
label.centerYAnchor.constraint(equalTo: container.centerYAnchor),
label.widthAnchor.constraint(equalTo: container.heightAnchor),
label.heightAnchor.constraint(equalTo: container.widthAnchor),
])
英文:
You should first create a label with a frame like the one in the second screenshot (width > height), then rotate that to get a frame like in the first screenshot (height > width).
let label = UILabel()
label.text = "The flower is beautiful"
label.backgroundColor = .yellow
label.textAlignment = .center
label.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(label)
NSLayoutConstraint.activate([
label.centerYAnchor.constraint(equalTo: view.centerYAnchor),
label.centerXAnchor.constraint(equalTo: view.centerXAnchor),
label.heightAnchor.constraint(equalToConstant: 40),
// note the label.width = view.height constraint here
label.widthAnchor.constraint(equalTo: view.heightAnchor)
])
label.transform = CGAffineTransform(rotationAngle: .pi / 2)
If you want to take the safe area into account, you can add a container view. The container view would have a frame similar to the first screenshot, and the label will still have a frame similar to the second screenshot. The container's top and bottom would be constrained to the safe area.
let label = UILabel()
label.text = "The flower is beautiful"
label.textAlignment = .center
label.translatesAutoresizingMaskIntoConstraints = false
label.transform = CGAffineTransform(rotationAngle: .pi / 2)
let container = UIView()
container.backgroundColor = .yellow
container.addSubview(label)
container.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(container)
NSLayoutConstraint.activate([
container.centerXAnchor.constraint(equalTo: view.centerXAnchor),
container.widthAnchor.constraint(equalToConstant: 40),
container.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor),
container.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
label.centerXAnchor.constraint(equalTo: container.centerXAnchor),
label.centerYAnchor.constraint(equalTo: container.centerYAnchor),
label.widthAnchor.constraint(equalTo: container.heightAnchor),
label.heightAnchor.constraint(equalTo: container.widthAnchor),
])
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论