我无法在Swift 5中使@KaanDedeoglu的解决方案适用于多行标签和文本视图 - 不知何故 - 所以我最终编写了一个“手动”解决方案,保持与@KaanDedeoglu答案中看到的相同的函数签名,供有兴趣的人使用。对于我的程序中的用途非常有效。
宽度
extension String {
func width(withConstrainedHeight height: CGFloat, font: UIFont) -> CGFloat {
var wordBoxes = [CGSize]()
var calculatedHeight = CGFloat.zero
var calculatedWidth = CGFloat.zero
for word in self.wordsWithWordSeparators() {
let box = word.boundingRect(with: CGSize.zero, attributes: [.font: font], context: nil)
let boxSize = CGSize(width: box.width, height: box.height)
wordBoxes.append(boxSize)
calculatedHeight += boxSize.height
calculatedWidth = calculatedWidth < boxSize.width ? boxSize.width : calculatedWidth
}
while calculatedHeight > height && wordBoxes.count > 1 {
var bestLineToRelocate = wordBoxes.count - 1
for i in 1..<wordBoxes.count {
let bestPotentialWidth = wordBoxes[bestLineToRelocate - 1].width + wordBoxes[bestLineToRelocate].width
let thisPotentialWidth = wordBoxes[i - 1].width + wordBoxes[i].width
if bestPotentialWidth > thisPotentialWidth {
bestLineToRelocate = i
}
}
calculatedHeight -= wordBoxes[bestLineToRelocate].height
wordBoxes[bestLineToRelocate - 1].width += wordBoxes[bestLineToRelocate].width
wordBoxes.remove(at: bestLineToRelocate)
calculatedWidth = max(wordBoxes[bestLineToRelocate - 1].width, calculatedWidth)
}
return ceil(calculatedWidth)
}
}
高度
extension String {
func height(withConstrainedWidth width: CGFloat, font: UIFont) -> CGFloat {
var wordBoxes = [CGSize]()
var calculatedHeight = CGFloat.zero
var currentLine = 0
for word in self.wordsWithWordSeparators() {
let box = word.boundingRect(with: CGSize.zero, attributes: [.font: font], context: nil)
let boxSize = CGSize(width: box.width, height: box.height)
if wordBoxes.isEmpty == true {
wordBoxes.append(boxSize)
}
else if wordBoxes[currentLine].width + boxSize.width > width {
wordBoxes.append(boxSize)
currentLine += 1
}
else {
wordBoxes[currentLine].width += boxSize.width
wordBoxes[currentLine].height = max(wordBoxes[currentLine].height, boxSize.height)
}
}
for wordBox in wordBoxes {
calculatedHeight += wordBox.height
}
return calculatedHeight
}
}
使用的辅助方法
extension String {
func wordsWithWordSeparators () -> [String] {
let range = self.startIndex..<self.endIndex
var words = [String]()
self.enumerateSubstrings(in: range, options: .byWords) { (substr, substrRange, enclosingRange, stop) in
let wordWithWordSeparators = String(self[enclosingRange])
words.append(wordWithWordSeparators)
}
return words
}
}
注意:这些高度和宽度的计算假定给定的标签或文本视图在执行换行时不会分割或连字符单词。如果您的情况不是这样,您只需要将单词替换为字符即可。此外,如果您处于运行时敏感环境中,可能需要考虑限制这些函数调用的速度或缓存结果,因为它们可能会根据字符串包含的单词数量有点昂贵。