Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/117.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在具有无限行的UILabel上换行?_Ios_Swift_Uilabel - Fatal编程技术网

Ios 如何在具有无限行的UILabel上换行?

Ios 如何在具有无限行的UILabel上换行?,ios,swift,uilabel,Ios,Swift,Uilabel,我有一个UILabel,其中的行数需要设置为0(无限行)。但是,当它显示一个长单词时,它会逐字符断开该行,形成第二行。我试图手动设置断线模式 cell.nameLbl.adjustsFontSizeToFitWidth = true cell.nameLbl.lineBreakMode = .byWordWrapping 但该应用程序仍按角色划分。我怎样才能解决这个问题? 编辑-我希望字号缩小。不幸的是,调整字号是按标签进行的,而不是按行。包装不会影响它。因此,您看到的结果是预期的。您可以手动

我有一个UILabel,其中的行数需要设置为0(无限行)。但是,当它显示一个长单词时,它会逐字符断开该行,形成第二行。我试图手动设置断线模式

cell.nameLbl.adjustsFontSizeToFitWidth = true
cell.nameLbl.lineBreakMode = .byWordWrapping
但该应用程序仍按角色划分。我怎样才能解决这个问题?
编辑-我希望字号缩小。

不幸的是,调整字号是按标签进行的,而不是按行。包装不会影响它。因此,您看到的结果是预期的。您可以手动将字符串拆分为多个标签,也可以将它们放入堆栈视图以获得所需的结果

我尝试的是:

private func layoutText(text: String, onStackView stackView: UIStackView) {
    var words: [String] = text.components(separatedBy: .whitespaces)

    var currentLineWords: [String] = [String]()
    var currentLabel: UILabel!

    func newLabel() -> UILabel {
        let label = UILabel(frame: stackView.bounds)
        label.adjustsFontSizeToFitWidth = true
        label.minimumScaleFactor = 0.5
        return label
    }

    while words.count > 0 {
        if currentLineWords.count == 0 {
            currentLabel = newLabel()
            currentLineWords.append(words.removeFirst())
        } else {
            let newText = currentLineWords.joined(separator: " ") + " " + words[0]
            currentLabel.text = newText
            currentLabel.sizeToFit()

            if currentLabel.bounds.width > stackView.bounds.width {
                // Break line
                currentLabel.text = currentLineWords.joined(separator: " ")
                stackView.addArrangedSubview(currentLabel)
                currentLabel = nil
                currentLineWords = [String]()
            } else {
                // All good
                currentLineWords.append(words.removeFirst())
            }
        }
    }

    if currentLineWords.count > 0 {
        currentLabel.text = currentLineWords.joined(separator: " ")
        stackView.addArrangedSubview(currentLabel)
    }
}
这项工作相当灵活,但:

  • 如果单词太长,即使缩小了也无法排成一行,则在末尾会显示“…”
  • 收缩线仍然具有相同的高度,而不是缩小
  • 我们只处理空格和插入后面的空格
前两个问题可能都可以通过手动
调整ntsizetofitwidth
解决。基本上一直减小字体大小,直到合适或达到一定的大小。如果遇到“特定大小”场景,则只需在标签上使用多行(将以字符分隔)

最后一个需要额外的努力

我仍然不会实现这一点,原因有很多,其中之一是它看起来很难看,但仍然很有趣

自定义字体大小调整:

要进行自定义字体调整和所有逻辑,只有在创建新留置权时才会进行更改:

            if currentLineWords.count == 0 {
                currentLabel = newLabel()
                currentLineWords.append(words.removeFirst())

                currentLabel.text = currentLineWords.joined(separator: " ")
                currentLabel.sizeToFit()

                if currentLabel.bounds.width > stackView.bounds.width {
                    currentLabel.adjustsFontSizeToFitWidth = false

                    let originalFontSize: CGFloat = currentLabel.font.pointSize
                    var currentFontSize: CGFloat = originalFontSize
                    let minimalFontSize: CGFloat = currentLabel.minimumScaleFactor > 0.0 ? currentLabel.font.pointSize * currentLabel.minimumScaleFactor : originalFontSize

                    while currentLabel.bounds.width > stackView.bounds.width {
                        currentFontSize -= 1.0
                        if currentFontSize < minimalFontSize {
                            currentLabel.numberOfLines = 0
                            currentLabel.font = UIFont(name: currentLabel.font.fontName, size: originalFontSize)
                            break // Could not shrink it even further. USe multiline
                        }
                        currentLabel.font = UIFont(name: currentLabel.font.fontName, size: currentFontSize)
                        currentLabel.sizeToFit()
                    }

                    // Break line
                    stackView.addArrangedSubview(currentLabel)
                    currentLabel = nil
                    currentLineWords = [String]()
                }
            } 
如果currentLineWords.count==0{
currentLabel=newLabel()
currentLineWords.append(words.removeFirst())
currentLabel.text=currentLineWords.joined(分隔符:“”)
currentLabel.sizeToFit()
如果currentLabel.bounds.width>stackView.bounds.width{
currentLabel.adjustsFontSizeToFitWidth=false
让originalFontSize:CGFloat=currentLabel.font.pointSize
var currentFontSize:CGFloat=originalFontSize
让minimalFontSize:CGFloat=currentLabel.MinimaumScaleFactor>0.0?currentLabel.font.pointSize*currentLabel.MinimaumScaleFactor:originalFontSize
而currentLabel.bounds.width>stackView.bounds.width{
currentFontSize-=1.0
如果currentFontSize
这看起来现在可以用了。我的结果是:


预期结果是什么?如果一个单词不能匹配,那么它将被更小的单位打断,在本例中,该单位是一个字符。添加了预期结果,尽管此网页将自动通过rappingwords拆分为多行,但您可以在我以前的评论中看到,它可能不适用于美国编写的整个社区,或者在这种情况下,它将在VerifitseesfitwrappingCharacter预期/希望的结果为,缩小字符。到最小字体大小。优先考虑的应该是,不要违背诺言。如果单词在最小字体大小之后仍然中断,它们可以在字符上中断。包装单词不会自动收缩单词。每个标签都会自动收缩。我不确定是否存在适合您需要的工具,但如果您已经查看了web,我会提出一个新的问题。