Skip to content

Instantly share code, notes, and snippets.

@JonFir
Last active April 10, 2017 04:06
Show Gist options
  • Save JonFir/dc1e793cb42e02279d6cb9858e5a504a to your computer and use it in GitHub Desktop.
Save JonFir/dc1e793cb42e02279d6cb9858e5a504a to your computer and use it in GitHub Desktop.
import UIKit
extension NSAttributedString {
static func makeAtributes(
font: UIFont,
lineSpacing: CGFloat? = nil,
characterSpacing: CGFloat? = nil,
color: UIColor? = nil
) -> [String: Any] {
var attrs: [String : Any] = [
NSFontAttributeName: font
]
if let lineSpacing = lineSpacing {
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = lineSpacing - font.lineHeight
attrs[NSParagraphStyleAttributeName] = paragraphStyle
}
if let characterSpacing = characterSpacing {
attrs[NSKernAttributeName] = characterSpacing
}
if let color = color {
attrs[NSForegroundColorAttributeName] = color
}
return attrs
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment