在Swift中垂直显示文本?

在Swift中垂直显示文本?,swift,text-alignment,Swift,Text Alignment,在我开始之前,我已经访问了 和 还有许多其他地方,但上面链接中列出的答案(或我去过的任何地方)都没有解决我的问题。所以,事情是这样的: 我试图在设备从纵向更改为横向后垂直显示UILabel中的文本 我通过改变帧大小并将numberOfLines设置为0,成功地(强制地)垂直堆叠了它们 这种方法产生了我想要的结果,除了旋转后每个字符之间的空格,大大延长了单词的长度 请查看下面的图片,以便更好地了解我的感受 理想情况下,旋转后不会出现不必要的空白,而且会像肖像画一样美观紧凑 有人知道我怎样才能做到

在我开始之前,我已经访问了 和 还有许多其他地方,但上面链接中列出的答案(或我去过的任何地方)都没有解决我的问题。所以,事情是这样的:

我试图在设备从纵向更改为横向后垂直显示UILabel中的文本

我通过改变帧大小并将numberOfLines设置为0,成功地(强制地)垂直堆叠了它们

这种方法产生了我想要的结果,除了旋转后每个字符之间的空格,大大延长了单词的长度

请查看下面的图片,以便更好地了解我的感受

理想情况下,旋转后不会出现不必要的空白,而且会像肖像画一样美观紧凑

有人知道我怎样才能做到这一点吗?在我的示例中,我使用UILabel,但是使用UITextView的答案也可以,只要它有效。下面是我的代码:

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)  
            }  
        }  
    }  

} 

更新:

经过进一步的调查,行距已经为零,但实际问题是行高

要解决此问题,请在
formatLabel
函数中调整行高

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  

    if let text = label.text {

        let attributedString = NSMutableAttributedString(string: text)

        let paragraphStyle = NSMutableParagraphStyle()

        let newLineHeight = (label.font.lineHeight / 3) * 2

        paragraphStyle.minimumLineHeight = newLineHeight
        paragraphStyle.maximumLineHeight = newLineHeight
        paragraphStyle.alignment = label.textAlignment

        attributedString.setAttributes([NSAttributedString.Key.paragraphStyle: paragraphStyle], range: NSRange(location: 0, length: text.count))

        label.attributedText = attributedString

    }

    positionView(label, isPortrait: UIApplication.shared.statusBarOrientation.isPortrait)  
}  

原始答案:

当标签宽度减小时,每个字母都放在自己的行上

行距对于阅读句子至关重要,但由于这些行是同一个单词的一部分,因此需要调整行距以使单词看起来相邻


关于UILabel封面行距的答案。

更新:

经过进一步的调查,行距已经为零,但实际问题是行高

要解决此问题,请在
formatLabel
函数中调整行高

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  

    if let text = label.text {

        let attributedString = NSMutableAttributedString(string: text)

        let paragraphStyle = NSMutableParagraphStyle()

        let newLineHeight = (label.font.lineHeight / 3) * 2

        paragraphStyle.minimumLineHeight = newLineHeight
        paragraphStyle.maximumLineHeight = newLineHeight
        paragraphStyle.alignment = label.textAlignment

        attributedString.setAttributes([NSAttributedString.Key.paragraphStyle: paragraphStyle], range: NSRange(location: 0, length: text.count))

        label.attributedText = attributedString

    }

    positionView(label, isPortrait: UIApplication.shared.statusBarOrientation.isPortrait)  
}  

原始答案:

当标签宽度减小时,每个字母都放在自己的行上

行距对于阅读句子至关重要,但由于这些行是同一个单词的一部分,因此需要调整行距以使单词看起来相邻


关于UILabel封面行距的答案。

我尝试了您链接中概述的解决方案,但没有成功(对原始行为没有任何更改)。这是令人惊讶的,因为我怀疑行距是罪魁祸首。你知道我做错了什么吗?还有:我是新来的。如果我想在回复中发布代码,我该怎么做?对于回复中的
code
,请使用“(”反勾号)打开和关闭代码块。我尝试了您链接中概述的解决方案,但没有成功(对原始行为没有更改)。这是令人惊讶的,因为我怀疑行距是罪魁祸首。你知道我做错了什么吗?还有:我是新来的。如果我想在回复中发布代码,我该怎么做?对于回复中的
code
,请用`(backtick)打开并关闭代码块。