Skip to content

Instantly share code, notes, and snippets.

@amayatsky
Forked from mkubenka/NSString+KBAdditions.h
Last active February 22, 2019 13:28
Show Gist options
  • Save amayatsky/e6125a2288cc2e4f1bbf to your computer and use it in GitHub Desktop.
Save amayatsky/e6125a2288cc2e4f1bbf to your computer and use it in GitHub Desktop.
Rewritten for Swift + UIButton extension
//
// 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;
}
}
//
// UIButton+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 UIButtonKBAdditions {
func sizeToFitMultipleLines(sizeToFit: Bool)
}
extension UIButton : UIButtonKBAdditions {
func sizeToFitMultipleLines(sizeToFit: Bool = false) {
if let label = self.titleLabel {
if label.adjustsFontSizeToFitWidth {
if let text = label.text {
let adjustedFontSize = text.fontSizeWithFont(label.font, constrainedToSize:self.frame.size, minimumScaleFactor:label.minimumScaleFactor)
label.font = label.font.fontWithSize(adjustedFontSize)
if sizeToFit {
self.sizeToFit()
}
}
}
}
}
}
//
// 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