Skip to content

Instantly share code, notes, and snippets.

@JesusMartinAlonso
Created July 11, 2019 12:24
Show Gist options
  • Save JesusMartinAlonso/388c86c8cf6e886984045b556fbf0866 to your computer and use it in GitHub Desktop.
Save JesusMartinAlonso/388c86c8cf6e886984045b556fbf0866 to your computer and use it in GitHub Desktop.
Some extension functions of UILabel that helps to format the text of the label
//
// UILabel+Utils.swift
//
// Created by Jesus Martin Alonso on 21/11/2018.
//
import Foundation
import UIKit
extension UILabel {
/// Set line spacing of attributedText keeping the previous attributes settings
///
/// - Parameter lineSpacing: CGFloat with the desired line spacing
func setLineSpacing(_ lineSpacing: CGFloat) {
guard let labelText = text, labelText.count > 0 else {
return
}
var attributes = attributedText?.attributes(at: 0, effectiveRange: nil) ?? [:]
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = lineSpacing // Line Spacing
paragraphStyle.alignment = .center
attributes[.paragraphStyle] = paragraphStyle
attributedText = NSAttributedString(string: labelText, attributes: attributes)
}
/// Underline text of attributedText keeping the previous attributes settings
func underline() {
guard let labelText = text, labelText.count > 0 else {
return
}
var attributes = attributedText?.attributes(at: 0, effectiveRange: nil) ?? [:]
attributes[.underlineStyle] = NSUnderlineStyle.single.rawValue
attributedText = NSAttributedString(string: labelText,
attributes: attributes)
}
/// Set letter spacing of attributedText keeping the previous attributes settings
///
/// - Parameter kernValue: Double with the kern value of the desired letter spacing
func setLetterSpacing(kernValue: Double) {
guard let labelText = text, labelText.count > 0 else {
return
}
var attributes = attributedText?.attributes(at: 0, effectiveRange: nil) ?? [:]
attributes[.kern] = kernValue
attributedText = NSAttributedString(string: labelText,
attributes: attributes)
}
/// Adjust the font size of the label to fit the text in the frame of the label
func adjustFontToFitText() {
guard let text = text else {
print("Text is nil")
return
}
self.adjustsFontSizeToFitWidth = false
self.font = maxFontToFitStringInFrame(font: font, string: text, frame: self.frame)
}
/// Override localize function to localize text of the label
public override func localize() {
super.localize()
localize(text) { text = $0 }
}
/// Function that returns the font with its max size to fit a given string in a given width
///
/// The function reduces the given font size until the given string fits in the given width
/// - Parameters:
/// - font: The font of the string.
/// - string: The string to fit in the frame
/// - frame: The frame
/// - Returns: The new UIFont that fits the given width with the given string
fileprivate func maxFontToFitStringInFrame(font: UIFont, string: String, frame: CGRect) -> UIFont {
var newFont = font
print("text of label: \(string)")
print("Original font: \(font)")
let myString: NSString = string as NSString
var widthOfText = myString.size(withAttributes: [NSAttributedString.Key.font: newFont]).width
var heightOfText = myString.size(withAttributes: [NSAttributedString.Key.font: newFont]).height
while frame.width < widthOfText || frame.height < heightOfText {
let fontSize = newFont.pointSize
newFont = newFont.withSize(fontSize - 0.5)
widthOfText = myString.size(withAttributes: [NSAttributedString.Key.font: newFont]).width
heightOfText = myString.size(withAttributes: [NSAttributedString.Key.font: newFont]).height
}
print("New font: \(newFont)")
return newFont
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment