Skip to content

Instantly share code, notes, and snippets.

@impul
Created January 20, 2020 14:04
Show Gist options
  • Save impul/d6f0cd3ac0366058454c08aea3bdc636 to your computer and use it in GitHub Desktop.
Save impul/d6f0cd3ac0366058454c08aea3bdc636 to your computer and use it in GitHub Desktop.
Swift text height calculation
import UIKit
import XCTest
import PlaygroundSupport
extension String {
func height(withWidth width: CGFloat, font: UIFont) -> CGFloat {
let linesToCalculate = components(separatedBy: "\n")
var textHeight: CGFloat = 0
linesToCalculate.forEach { string in
let maxSize = CGSize(width: width, height: CGFloat.greatestFiniteMagnitude)
let actualSize = string.boundingRect(with: maxSize, options: [.usesLineFragmentOrigin], attributes: [NSAttributedString.Key.font: font], context: nil)
textHeight += actualSize.height
}
return textHeight
}
}
let textWidth: CGFloat = 100
let defaultTextHeight: CGFloat = 334.140625
let textWithNewLinesHeight: CGFloat = 350.84765625
let emptyTextHeight: CGFloat = 16.70703125
let frenceTextHeight: CGFloat = 284.01953125
let defaultText = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."
let textWithNewLines = "Lorem ipsum\n dolor sit amet,\n consectetur adipiscing elit,\n sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.\n Ut enim ad minim veniam,\n quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. "
let frenchText = "Informations détaillées sur le niveau de sécurité de chaque trajet. Obtenez des alertes en cas de conduite agressive, d'accélération rapide, de freinage brutal, de pointes de vitesse ou d'utilisation du téléphone."
let font = UIFont.systemFont(ofSize: 14, weight: .semibold)
func testDefaultTextCalculation() {
let height = defaultText.height(withWidth: textWidth, font: font)
let heightWithNewLines = textWithNewLines.height(withWidth: textWidth, font: font)
let emtyText = "".height(withWidth: textWidth, font: font)
XCTAssertEqual(height, defaultTextHeight)
XCTAssertEqual(heightWithNewLines, textWithNewLinesHeight)
XCTAssertEqual(emtyText, emptyTextHeight)
}
let frenchTextHeight = frenchText.height(withWidth: textWidth, font: font)
let label = UILabel(frame: CGRect(origin: .zero, size: CGSize(width: textWidth, height: frenchTextHeight)))
label.text = frenchText
label.backgroundColor = .white
label.font = font
label.numberOfLines = 0
//PlaygroundPage.current.liveView = label
let textView = UITextView(frame: CGRect(origin: .zero, size: CGSize(width: textWidth, height: defaultTextHeight)))
textView.text = defaultText
textView.font = font
textView.contentInset
//textView.textContainer.lineFragmentPadding = 0
//textView.textContainerInset = .zero
PlaygroundPage.current.liveView = textView
testDefaultTextCalculation()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment