Skip to content

Instantly share code, notes, and snippets.

@khanlou
Created December 7, 2016 19:23
Show Gist options
  • Select an option

  • Save khanlou/fc64e057153d6ef8788cf8f61d1abae2 to your computer and use it in GitHub Desktop.

Select an option

Save khanlou/fc64e057153d6ef8788cf8f61d1abae2 to your computer and use it in GitHub Desktop.
HeaderFormatter, through the refactorings
struct HeaderFormatter {
var text: String
var attributedString: NSAttributedString? {
guard let font = UIFont.champion(ofSize: 32) else { return nil }
let attributes: [NSString : AnyObject] = [NSFontAttributeName as NSString: font, NSForegroundColorAttributeName as NSString: UIColor.black, NSKernAttributeName as NSString : 1.5 as AnyObject]
return NSAttributedString(string: text, attributes: attributes as [String : Any]?)
}
}
struct HeaderFormatter {
var text: String
var attributedString: NSAttributedString? {
guard let font = UIFont.champion(ofSize: 30) else { return nil }
let attributes: [String : AnyObject] = [
NSFontAttributeName as String: font,
NSForegroundColorAttributeName as String: UIColor.black,
NSKernAttributeName as String : 1.5 as AnyObject
]
return NSAttributedString(string: text.uppercased(), attributes: attributes)
}
}
struct SubtitleFormatter {
var text: String
var attributedString: NSAttributedString? {
guard let font = UIFont.dinBold(ofSize: 14) else { return nil }
let attributes: [String : AnyObject] = [
NSFontAttributeName as String: font,
NSForegroundColorAttributeName as String: UIColor.lightGray
]
return NSAttributedString(string: text.uppercased(), attributes: attributes)
}
}
enum TitleType {
case header
case subtitle
}
struct TitleFormatter {
var text: String
var type: TitleType
var attributedString: NSAttributedString? {
switch type {
case .header:
guard let font = UIFont.champion(ofSize: 30) else { return nil }
let attributes: [String : AnyObject] = [
NSFontAttributeName as String: font,
NSForegroundColorAttributeName as String: UIColor.black,
NSKernAttributeName as String : 1.5 as AnyObject
]
return NSAttributedString(string: text.uppercased(), attributes: attributes)
case .subtitle:
guard let font = UIFont.dinBold(ofSize: 14) else { return nil }
let attributes: [String : AnyObject] = [
NSFontAttributeName as String: font,
NSForegroundColorAttributeName as String: UIColor.lightGray
]
return NSAttributedString(string: text.uppercased(), attributes: attributes)
}
}
}
//
// TitleFormatter.swift
// Urban Archive
//
// Created by Katie Smillie on 10/10/16.
// Copyright © 2016 Urban Archive. All rights reserved.
//
import Foundation
import UIKit
enum TextType {
case smallTitle
case bodyText
case pageTitle
var font: UIFont {
switch self {
case .smallTitle:
return UIFont.dinBold(ofSize: 8)
case .bodyText:
return UIFont.dinLight(ofSize: 14)
case .pageTitle:
return UIFont.dinBold(ofSize: 28)
}
}
var attributes: [String : AnyObject]? {
switch self {
case .smallTitle:
return [NSFontAttributeName as String: font,
NSForegroundColorAttributeName as String: UIColor.uaLightGray(),
NSKernAttributeName as String : 1.3 as AnyObject]
case .bodyText:
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = 4
return [NSFontAttributeName as String: font,
NSParagraphStyleAttributeName as String: paragraphStyle,
NSForegroundColorAttributeName as String: UIColor.uaGray()]
case .pageTitle:
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = 0.8
return [NSFontAttributeName as String: font,
NSParagraphStyleAttributeName as String: paragraphStyle,
NSForegroundColorAttributeName as String: UIColor.black]
}
}
}
struct TextFormatter {
enum FontCase {
case noChange
case uppercase
func transform(_ string: String) -> String {
switch self {
case .noChange: return string
case .uppercase: return string.uppercased()
}
}
}
var text: String
var type: TextType
var fontCase: FontCase
init(text: String, type: TextType, fontCase: FontCase = .noChange) {
self.text = text
self.type = type
self.fontCase = fontCase
}
var attributedString: NSAttributedString? {
return NSAttributedString(string: fontCase.transform(text), attributes: type.attributes)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment