我有一个带有一些属性文本的UITextView
,其中设置了textContainer.maximumNumberOfLine
(在这种情况下为3)。
我想在属性字符串的字符范围内找到省略号字符的索引。
E.g:
原始字符串:
"Lorem ipsum dolor sit amet, consectetur adipiscing elit"
截断后显示的字符串:
Lorem ipsum
dolor sit amet,
consectetur...
如何确定...
的索引?
答案 0 :(得分:3)
这是NSAttributedString的扩展功能,可以完成这项工作。适用于单身和单人多行文字。
这花了我大约8个小时来弄明白,所以我想我会把它作为Q& A发布。
(Swift 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
}
请注意,除了一些简单的情况,这还没有经过测试。可能有边缘情况需要进行一些调整..
答案 1 :(得分:1)
我用Swift 5更新了@Tim Malseed的解决方案
extension UILabel {
var truncationIndex: Int? {
guard let text = text else {
return nil
}
let attributes: [NSAttributedString.Key: UIFont] = [.font: font]
let attributedString = NSAttributedString(string: text, attributes: attributes)
let textContainer = NSTextContainer(
size: CGSize(width: frame.size.width,
height: CGFloat.greatestFiniteMagnitude)
)
textContainer.maximumNumberOfLines = numberOfLines
textContainer.lineBreakMode = NSLineBreakMode.byTruncatingTail
let layoutManager = NSLayoutManager()
layoutManager.addTextContainer(textContainer)
let textStorage = NSTextStorage(attributedString: attributedString)
textStorage.addLayoutManager(layoutManager)
//Determine the range of all Glpyhs within the string
var glyphRange = NSRange()
layoutManager.glyphRange(
forCharacterRange: NSMakeRange(0, attributedString.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.enumerateLineFragments(
forGlyphRange: glyphRange
) { rect, usedRect, textContainer, glyphRange, stop in
if (i == self.numberOfLines - 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.truncatedGlyphRange(inLineFragmentForGlyphAt: lineFragmentTruncatedGlyphIndex).location
}
stop.pointee = true
}
i += 1
}
return truncationIndex
}
}