如何旋转标签中的内容?

huangapple go评论112阅读模式
英文:

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),
])

如何旋转标签中的内容?

huangapple
  • 本文由 发表于 2023年8月10日 11:40:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76872513.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定