Skip to content

Instantly share code, notes, and snippets.

@diegosanchezr
Created May 10, 2016 23:31
Show Gist options
  • Save diegosanchezr/15c560adaec41472ced2e02ae1cc6d64 to your computer and use it in GitHub Desktop.
Save diegosanchezr/15c560adaec41472ced2e02ae1cc6d64 to your computer and use it in GitHub Desktop.
Text sizing comparison
struct Result: CustomStringConvertible {
let size: CGSize
let detail: String
var description: String {
return "\(size) \(detail)"
}
}
let texts: [String] = [
"Lorem ipsum dolor sit amet 😇, https://github.com/badoo/Chatto consectetur adipiscing elit , sed do eiusmod tempor incididunt 07400000000 📞 ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute ir incoming:incoming, #:7",
"Говори мне что я уже и не надо так говорить о своих о том же что как только мы я он не был хочет может и нет не правда это не или нет и нет ни одного не человека из них были не в курсе что в этом результате которого я в шоке с от была в школе шоке том числе в и на этом месте фоне моменте я уже не будет в ни одного человека из головы не вылетело и я тебя люблю я и щне не в знаю почему но так лень идти вставать и идти ехать домой и в с шах планете в том числе и на этот раз счёт в серии банке в можно взять шаг в этом сторону от и утр в и не только в надо будет было не в состоянии здоровья тебе в не в состоянии здоровья в тебе не кажется нравится этот день раз по эксплуатации на автомобиля и не только на один день из в контакте",
"Lorem ipsum dolor sit amet 😇, https://github.com/badoo/Chatto consectetur adipiscing elit , sed do eiusmod tempor ੨ 07400000000 📞 ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute ir incoming:incoming, #:7",
"駓駗鴀 珿祪 煻, 硻禂稢 脀蚅蚡 蔝蓶 賗 虥諰 壾嵷幓 蠿饡驦 摓, 暲 羳蟪蠁 秎穾籺 藽轚酁 昢炾 蔍 萷葋蒎 熿熼燛 緷緦, 滈溔滆 螒螝螜 姴怤昢 鑆驈 瑽, 讘麡 絼 轒醭鏹 嗛嗕塨 豲貕貔 檓檌 硻禂稢 鼥儴壛 踙 摨敹暯 蠬襱覾 雥齆犪 楋 毚丮, 孻憵懥 耜僇鄗 憢憉 慛, 钀钁 慛 鏊"
]
let cfURL = NSBundle.mainBundle().URLForResource("FS Albert Pro", withExtension: "otf")! //as! CFURL
CTFontManagerRegisterFontsForURL(cfURL, CTFontManagerScope.Process, nil)
let fonts = [
UIFont.systemFontOfSize(16),
UIFont(name: "FSAlbertPro", size: 16)!,
]
print("START")
for font in fonts {
let attributes = [
NSFontAttributeName: font,
NSKernAttributeName: 0,
]
for text in texts {
for i in 100...300 {
var results = [Result]()
let size = CGSize(width: CGFloat(i), height: .max)
do {
// NSLayoutManager
let textContainer = NSTextContainer(size: size)
let layoutManager = NSLayoutManager()
let textStorage = NSTextStorage(string: text, attributes: attributes)//NSTextStorage(attributedString: attributedText)
textContainer.lineFragmentPadding = 0
layoutManager.addTextContainer(textContainer)
textStorage.addLayoutManager(layoutManager)
let rect = layoutManager.usedRectForTextContainer(textContainer)
results.append(Result(size: rect.size.bma_round(), detail: "NSLayoutManager"))
}
do {
// UILabel - attributed
let label = UILabel()
let attributedString = NSAttributedString(string: text, attributes: attributes)
label.attributedText = attributedString
label.numberOfLines = 0
let size = label.sizeThatFits(size)
// results.append(Result(size: size.bma_round(), detail: "UILabel-attributed"))
}
do {
// UILabel - text
let label = UILabel()
label.font = font
label.text = text
label.numberOfLines = 0
let size = label.sizeThatFits(size)
// results.append(Result(size: size.bma_round(), detail: "UILabel-String"))
}
do {
let textView = UITextView()
textView.dataDetectorTypes = .None
let textStorage = NSTextStorage(string: text, attributes: attributes)
textView.attributedText = textStorage
textView.textContainerInset = UIEdgeInsetsZero
textView.layoutManager.allowsNonContiguousLayout = true
textView.textContainer.lineFragmentPadding = 0
let size = textView.sizeThatFits(size)
results.append(Result(size: size.bma_round(), detail: "UITextView-NSTextStorage"))
}
do {
let textView = UITextView()
textView.dataDetectorTypes = .None
let attributedString = NSAttributedString(string: text, attributes: attributes)
textView.attributedText = attributedString
textView.textContainerInset = UIEdgeInsetsZero
textView.layoutManager.allowsNonContiguousLayout = true
textView.textContainer.lineFragmentPadding = 0
let size = textView.sizeThatFits(size)
// results.append(Result(size: size.bma_round(), detail: "UITextView-NSAttributedString"))
}
do {
let textView = UITextView()
textView.text = text
textView.font = font
textView.textContainerInset = UIEdgeInsetsZero
textView.textContainer.lineFragmentPadding = 0
textView.layoutManager.allowsNonContiguousLayout = true
let size = textView.sizeThatFits(size)
// results.append(Result(size: size.bma_round(), detail: "UITextView-text"))
}
do {
// String
let rect = text.boundingRectWithSize(size, options:[.UsesLineFragmentOrigin, .UsesFontLeading], attributes: attributes, context: nil)
// results.append(Result(size: rect.size.bma_round(), detail: "String"))
}
do { // Attributed string
let attributedString = NSAttributedString(string: text, attributes: attributes)
let rect = attributedString.boundingRectWithSize(size, options: [.UsesLineFragmentOrigin, .UsesFontLeading], context: nil)
// results.append(Result(size: rect.size.bma_round(), detail: "AttributedString"))
}
do {
let textStorage = NSTextStorage(string: text, attributes: attributes)
let rect = textStorage.boundingRectWithSize(size, options: [.UsesLineFragmentOrigin, .UsesFontLeading], context: nil)
// results.append(Result(size: rect.size.bma_round(), detail: "NSTextStorage"))
}
var differ = false
outer: for result_i in results {
for result_j in results {
if !result_i.size.bma_equal(result_j.size) {
differ = true
break outer
}
}
}
if differ {
print("MISMATCH")
print("text", text)
print("font", font)
print("width", size.width)
for result in results {
print(result)
}
}
}
}
}
print("END")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment