Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// Some of the things I use in my projects
import UIKit
let sharedApp = UIApplication.shared
let noteCenter = NotificationCenter.default
let userDefaults = UserDefaults.standard
func assertOnMain() { assert(Thread.isMainThread) }
func assertOffMain() { assert(!Thread.isMainThread) }
func assertFail() { assert(false) }
struct ScreenSize {
static let width = UIScreen.main.bounds.size.width
static let height = UIScreen.main.bounds.size.height
static let maxLength = max(ScreenSize.width, ScreenSize.height)
static let minLength = min(ScreenSize.width, ScreenSize.height)
}
struct DeviceType {
static let iPhone4orLess = UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.maxLength < 568.0
static let iPhone5 = UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.maxLength == 568.0
static let iPhone6 = UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.maxLength == 667.0
static let iPhone6p = UIDevice.current.userInterfaceIdiom == .phone && ScreenSize.maxLength == 736.0
}
extension UIFont {
class func slim(fontSize: CGFloat) -> UIFont {
return UIFont(name: "Avenir-Roman", size: fontSize)!
}
class func bold(fontSize: CGFloat) -> UIFont {
return UIFont(name: "Avenir-Black", size: fontSize)!
}
}
extension UILabel {
convenience init (frame: CGRect, text: String, textColor: UIColor, font: UIFont, alignment: NSTextAlignment, spacing: Bool) {
self.init()
self.frame = frame
self.text = text
self.textColor = textColor
self.font = font
self.textAlignment = alignment
if spacing {
self.setTextSpacing(spacing: 1.15)
}
}
func setTextSpacing(spacing: CGFloat){ // http://stackoverflow.com/a/36822550/611879
let attributedString = NSMutableAttributedString(string: text!)
if textAlignment == .center || textAlignment == .right {
attributedString.addAttribute(NSKernAttributeName, value: spacing, range: NSRange(location: 0, length: attributedString.length-1))
} else {
attributedString.addAttribute(NSKernAttributeName, value: spacing, range: NSRange(location: 0, length: attributedString.length))
}
attributedText = attributedString
}
func setText(string: String!, alignment: NSTextAlignment, spacing: CGFloat){
if string != nil && string.characters.count > 1 {
text = string
textAlignment = alignment
setTextSpacing(spacing: spacing)
}
}
static func navTitleLabel(text: String) -> UILabel {
let titleLabel = UILabel(frame: CGRect(x: 50, y: 0, width: ScreenSize.width-100, height: 13))
titleLabel.text = text
titleLabel.textColor = UIColor.white
titleLabel.font = UIFont.bold(fontSize: 15)
titleLabel.setTextSpacing(spacing: 1.15)
titleLabel.sizeToFit()
return titleLabel
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment