-
-
Save amayatsky/e6125a2288cc2e4f1bbf to your computer and use it in GitHub Desktop.
Rewritten for Swift + UIButton extension
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// NSString+KBAdditions.swift | |
// | |
// Created by Alexander Mayatsky on 16/03/16. | |
// | |
// Original code from http://stackoverflow.com/a/4383281/463892 & http://stackoverflow.com/a/18951386 | |
// | |
import Foundation | |
import UIKit | |
protocol NSStringKBAdditions { | |
func fontSizeWithFont(font: UIFont, constrainedToSize size: CGSize, minimumScaleFactor: CGFloat) -> CGFloat | |
} | |
extension NSString : NSStringKBAdditions { | |
func fontSizeWithFont(font: UIFont, constrainedToSize size: CGSize, minimumScaleFactor: CGFloat) -> CGFloat { | |
var fontSize = font.pointSize | |
let minimumFontSize = fontSize * minimumScaleFactor | |
var attributedText = NSAttributedString(string: self as String, attributes:[NSFontAttributeName: font]) | |
var height = attributedText.boundingRectWithSize(CGSize(width: size.width, height: CGFloat.max), options:NSStringDrawingOptions.UsesLineFragmentOrigin, context:nil).size.height | |
var newFont = font | |
//Reduce font size while too large, break if no height (empty string) | |
while (height > size.height && height != 0 && fontSize > minimumFontSize) { | |
fontSize--; | |
newFont = UIFont(name: font.fontName, size: fontSize)! | |
attributedText = NSAttributedString(string: self as String, attributes:[NSFontAttributeName: newFont]) | |
height = attributedText.boundingRectWithSize(CGSize(width: size.width, height: CGFloat.max), options:NSStringDrawingOptions.UsesLineFragmentOrigin, context:nil).size.height | |
} | |
// Loop through words in string and resize to fit | |
for word in self.componentsSeparatedByCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet()) { | |
var width = word.sizeWithAttributes([NSFontAttributeName:newFont]).width | |
while (width > size.width && width != 0 && fontSize > minimumFontSize) { | |
fontSize-- | |
newFont = UIFont(name: font.fontName, size: fontSize)! | |
width = word.sizeWithAttributes([NSFontAttributeName:newFont]).width | |
} | |
} | |
return fontSize; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// UILabel+KBAdditions.swift | |
// | |
// Created by Alexander Mayatsky on 16/03/16. | |
// | |
// Original code from http://stackoverflow.com/a/4383281/463892 & http://stackoverflow.com/a/18951386 | |
// | |
import Foundation | |
import UIKit | |
protocol UILabelKBAdditions { | |
func sizeToFitMultipleLines() | |
} | |
extension UILabel : UILabelKBAdditions { | |
func sizeToFitMultipleLines() { | |
if self.adjustsFontSizeToFitWidth { | |
if let text = self.text { | |
let adjustedFontSize = text.fontSizeWithFont(self.font, constrainedToSize:self.frame.size, minimumScaleFactor:self.minimumScaleFactor) | |
self.font = self.font.fontWithSize(adjustedFontSize) | |
self.sizeToFit() | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment