Files
Swiftgram/submodules/Display/Display/Font.swift
Peter 8f5a4f7dc1 Add 'submodules/Display/' from commit '7bd11013ea936e3d49d937550d599f5816d32560'
git-subtree-dir: submodules/Display
git-subtree-mainline: 9bc996374f
git-subtree-split: 7bd11013ea
2019-06-11 18:44:37 +01:00

85 lines
3.2 KiB
Swift

import Foundation
import UIKit
public struct Font {
public static func regular(_ size: CGFloat) -> UIFont {
return UIFont.systemFont(ofSize: size)
}
public static func medium(_ size: CGFloat) -> UIFont {
if #available(iOS 8.2, *) {
return UIFont.systemFont(ofSize: size, weight: UIFont.Weight.medium)
} else {
return CTFontCreateWithName("HelveticaNeue-Medium" as CFString, size, nil)
}
}
public static func semibold(_ size: CGFloat) -> UIFont {
if #available(iOS 8.2, *) {
return UIFont.systemFont(ofSize: size, weight: UIFont.Weight.semibold)
} else {
return CTFontCreateWithName("HelveticaNeue-Medium" as CFString, size, nil)
}
}
public static func bold(_ size: CGFloat) -> UIFont {
if #available(iOS 8.2, *) {
return UIFont.boldSystemFont(ofSize: size)
} else {
return CTFontCreateWithName("HelveticaNeue-Bold" as CFString, size, nil)
}
}
public static func light(_ size: CGFloat) -> UIFont {
if #available(iOS 8.2, *) {
return UIFont.systemFont(ofSize: size, weight: UIFont.Weight.light)
} else {
return CTFontCreateWithName("HelveticaNeue-Light" as CFString, size, nil)
}
}
public static func semiboldItalic(_ size: CGFloat) -> UIFont {
if let descriptor = UIFont.systemFont(ofSize: size).fontDescriptor.withSymbolicTraits([.traitBold, .traitItalic]) {
return UIFont(descriptor: descriptor, size: size)
} else {
return UIFont.italicSystemFont(ofSize: size)
}
}
public static func monospace(_ size: CGFloat) -> UIFont {
return UIFont(name: "Menlo-Regular", size: size - 1.0) ?? UIFont.systemFont(ofSize: size)
}
public static func semiboldMonospace(_ size: CGFloat) -> UIFont {
return UIFont(name: "Menlo-Bold", size: size - 1.0) ?? UIFont.systemFont(ofSize: size)
}
public static func italicMonospace(_ size: CGFloat) -> UIFont {
return UIFont(name: "Menlo-Italic", size: size - 1.0) ?? UIFont.systemFont(ofSize: size)
}
public static func semiboldItalicMonospace(_ size: CGFloat) -> UIFont {
return UIFont(name: "Menlo-BoldItalic", size: size - 1.0) ?? UIFont.systemFont(ofSize: size)
}
public static func italic(_ size: CGFloat) -> UIFont {
return UIFont.italicSystemFont(ofSize: size)
}
}
public extension NSAttributedString {
convenience init(string: String, font: UIFont? = nil, textColor: UIColor = UIColor.black, paragraphAlignment: NSTextAlignment? = nil) {
var attributes: [NSAttributedStringKey: AnyObject] = [:]
if let font = font {
attributes[NSAttributedStringKey.font] = font
}
attributes[NSAttributedStringKey.foregroundColor] = textColor
if let paragraphAlignment = paragraphAlignment {
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = paragraphAlignment
attributes[NSAttributedStringKey.paragraphStyle] = paragraphStyle
}
self.init(string: string, attributes: attributes)
}
}