【问题标题】:Display text vertically in Swift?在 Swift 中垂直显示文本?
【发布时间】:2019-11-18 11:36:58
【问题描述】:

在开始之前,我访问过How to align text inside textView vertically?How can you rotate text for UIButton and UILabel in Swift? 和许多其他地方,但上述链接(或我去过的任何地方)中列出的答案都没有解决我的问题。所以事情是这样的:

在设备从纵向变为横向后,我试图在 UILabel 中垂直显示文本。

我已经设法(强制)通过更改帧大小和设置 numberOfLines = 0 垂直堆叠它们。

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

请查看下面的图片以更好地了解我的体验。

Spaces between Chars

理想情况下,旋转后不会出现不必要的空白,并且会像纵向一样美观紧凑。

有谁知道我如何做到这一点?在我的示例中,我使用的是 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)  
            }  
        }  
    }  

} 

【问题讨论】:

    标签: swift text-alignment


    【解决方案1】:

    更新:

    经过一番调查,行距已经为零,但实际上是行高。

    要解决这个问题,请在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)  
    }  
    

    原答案:

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

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

    关于this question UILabel 的覆盖行间距的答案。

    【讨论】:

    • 我已经尝试了您链接中列出的解决方案,但它不起作用(原始行为没有改变)。这很令人惊讶,因为我怀疑行距是我自己的罪魁祸首。你知道我做错了什么吗?另外:我是新来的。如果我想在回复中发布代码,我该怎么做?
    • 对于code的回复,用`(反引号)打开和关闭代码块。
    猜你喜欢
    • 2012-02-25
    • 1970-01-01
    • 2011-03-31
    • 1970-01-01
    • 2019-02-09
    • 2012-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多