UITextView:在截断文本中查找省略号的位置

Tim*_*eed 3 truncation uitextview nsattributedstring ios

我有一个UITextView带有一些属性文本的文本,其中textContainer.maximumNumberOfLine设置了(在这种情况下为 3)。

我想在属性字符串的字符范围内找到省略号字符的索引。

例如:

原始字符串:

"Lorem ipsum dolor sit amet, consectetur adipiscing elit"

截断后显示的字符串:

Lorem ipsum dolor sit amet, consectetur...

我如何确定 的索引...

Tim*_*eed 5

这是完成这项工作的 NSAttributedString 的扩展函数。适用于单行和多行文本。

这花了我大约 8 个小时才弄明白,所以我想我会把它作为问答发布。

(斯威夫特 2.2)

/**
    Returns the index of the ellipsis, if this attributed string is truncated, or NSNotFound otherwise.
*/
func truncationIndex(maximumNumberOfLines: Int, width: CGFloat) -> Int {

    //Create a dummy text container, used for measuring & laying out the text..

    let textContainer = NSTextContainer(size: CGSize(width: width, height: CGFloat.max))
    textContainer.maximumNumberOfLines = maximumNumberOfLines
    textContainer.lineBreakMode = NSLineBreakMode.ByTruncatingTail

    let layoutManager = NSLayoutManager()
    layoutManager.addTextContainer(textContainer)

    let textStorage = NSTextStorage(attributedString: self)
    textStorage.addLayoutManager(layoutManager)

    //Determine the range of all Glpyhs within the string

    var glyphRange = NSRange()
    layoutManager.glyphRangeForCharacterRange(NSMakeRange(0, self.length), actualCharacterRange: &glyphRange)

    var truncationIndex = NSNotFound

    //Iterate over each 'line fragment' (each line as it's presented, according to your `textContainer.lineBreakMode`)
    var i = 0
    layoutManager.enumerateLineFragmentsForGlyphRange(glyphRange) { (rect, usedRect, textContainer, glyphRange, stop) in
        if (i == maximumNumberOfLines - 1) {

            //We're now looking at the last visible line (the one at which text will be truncated)

            let lineFragmentTruncatedGlyphIndex = glyphRange.location
            if lineFragmentTruncatedGlyphIndex != NSNotFound {
                truncationIndex = layoutManager.truncatedGlyphRangeInLineFragmentForGlyphAtIndex(lineFragmentTruncatedGlyphIndex).location
            }
            stop.memory = true
        }
        i += 1
    }

    return truncationIndex
}
Run Code Online (Sandbox Code Playgroud)

请注意,除了一些简单的情况外,这还没有经过测试。可能存在需要一些调整的边缘情况。

  • @TimMalseed 好吧,在研究了半天之后,我了解到 `firstUnlaidCharacter()` 和 `firstUnlaidGlyph()` 的工作原理与迭代每个行片段一样,无论如何它都没有运行。另外,我必须将换行模式更改为“.byWordWrap”。但除此之外,这确实很有帮助。 (3认同)