Skip to content

Instantly share code, notes, and snippets.

@donholly
Last active March 8, 2016 00:42
Show Gist options
  • Save donholly/8d42f88c901912773228 to your computer and use it in GitHub Desktop.
Save donholly/8d42f88c901912773228 to your computer and use it in GitHub Desktop.
Find a minimum font size for a collection of strings (untested)
func minimumFontSizeForTitles(titles: [String], boundingSize: CGSize) -> CGFloat {
var minimumFontSize: CGFloat = 100
let labelForSizing = UILabel(frame: CGRect(origin: CGPointZero, size: boundingSize))
labelForSizing.font = UIFont.systemFontOfSize(minimumFontSize)
labelForSizing.adjustsFontSizeToFitWidth = true
for title in titles {
labelForSizing.text = title
minimumFontSize = min(minimumFontSize, labelForSizing.font.pointSize)
}
return minimumFontSize
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment