Skip to content

Instantly share code, notes, and snippets.

@bdalziel
Created June 7, 2016 17:36
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bdalziel/e640d5de6db89113429d0de91dd704ee to your computer and use it in GitHub Desktop.
Save bdalziel/e640d5de6db89113429d0de91dd704ee to your computer and use it in GitHub Desktop.
//
// FontUtil.swift
// FantasyMovieLeague
//
// Created by Ben Dalziel on 2/17/16.
// Copyright © 2016 Kinetoplay. All rights reserved.
//
import UIKit
public indirect enum AppUIFonts: Int {
case NavBarTitle,
PickerNavBarTitle,
Button,
BarButton,
BarButtonPrimary,
ToolTipButton,
TableSectionHeader,
TableCell,
TabBarItem,
SegmentedControl,
TabbedSegmentedControl,
CineplexScreen,
CineplexScreenMoviePrice,
Toast,
OnboardingQuestion,
RefreshControl,
EntryWeekHeading,
InsiderTitle,
InsiderHeader,
InsiderByline,
InsiderDescription,
InsiderBody,
SimpleText,
SimpleRegularText,
Form,
FormErrorMessage,
LeagueMembers,
TeamName,
TeamRank,
TeamPoints,
TeamStandingsPoints,
UserStatHeading,
UserStatHeadingBold,
UserStatValue,
MovieStatValue,
PickerMovieTitle,
LeagueName,
LeagueDirectoryName,
TopicTitle,
TopicByLine,
TopicCommentCount,
TopicPostDate,
TopicBody,
TopicCommentBody,
TopicCommentPostDate,
TipTitle,
TipDescription,
TipIndex,
NotificationLabel
}
class FontUtil {
enum AppFonts: String {
case AvertaThin = "Averta-Light",
AvertaRegular = "Averta-Regular",
WeissenhofThin = "WeissenhofGrotesk-Light",
Mono = "Courier New"
}
class func uiFont(appUIFont: AppUIFonts) -> UIFont {
switch appUIFont {
case .PickerNavBarTitle :
return self.getWeissenhofThin(size: 26.0)
case .UserStatValue,
.LeagueName:
return self.getWeissenhofThin(size: 24.0)
case .MovieStatValue,
.InsiderTitle,
.InsiderHeader,
.TipTitle,
.TopicTitle,
.PickerMovieTitle,
.Toast,
.OnboardingQuestion,
.LeagueDirectoryName:
return self.getWeissenhofThin(size: 20.0)
case .TeamName,
.TeamRank,
.TeamPoints:
return self.getWeissenhofThin(size: 16.0)
case .NavBarTitle,
.EntryWeekHeading:
return self.getAvertaThin(size: 16.0)
case .LeagueMembers,
.UserStatHeading,
.CineplexScreenMoviePrice,
.NotificationLabel,
.ToolTipButton,
.FormErrorMessage:
return self.getAvertaThin(size: 11.0)
case .InsiderDescription,
.SimpleText:
return self.getAvertaThin(size: 13.0)
case .TopicBody,
.TopicCommentBody,
.InsiderBody:
return self.getAvertaThin(size: 14.0)
case .TableCell,
.TipIndex,
.TopicPostDate,
.TabbedSegmentedControl:
return self.getAvertaThin(size: 11.0)
case .TopicCommentCount :
return self.getAvertaThin(size: 12.0)
case .SegmentedControl,
.Button,
.BarButton,
.BarButtonPrimary,
.InsiderByline,
.TableSectionHeader,
.TopicByLine,
.RefreshControl,
.UserStatHeadingBold:
return self.getAvertaRegular(size: 11.0)
case .SimpleRegularText :
return self.getAvertaRegular(size: 13.0)
case .TopicCommentPostDate :
return self.getAvertaThin(size: 9.0)
case .TabBarItem,
.CineplexScreen :
return self.getAvertaThin(size: 8.0)
case .Form,
.TipDescription:
return self.getAvertaThin(size: 14.0)
case .TeamStandingsPoints:
return self.getMono(size: 16.0)
}
}
class func getAvertaThin(size fontSize: CGFloat) -> UIFont {
return UIFont(name: AppFonts.AvertaThin.rawValue, size: fontSize)!
}
class func getAvertaRegular(size fontSize: CGFloat) -> UIFont {
return UIFont(name: AppFonts.AvertaRegular.rawValue, size: fontSize)!
}
class func getWeissenhofThin(size fontSize: CGFloat) -> UIFont {
return UIFont(name: AppFonts.WeissenhofThin.rawValue, size: fontSize)!
}
class func getMono(size fontSize: CGFloat) -> UIFont {
return UIFont(name: AppFonts.Mono.rawValue, size: fontSize)!
}
// http://giordanoscalzo.tumblr.com/post/95900320382/print-all-ios-fonts-in-swift
class func printFonts() {
let fontFamilyNames = UIFont.familyNames()
for familyName in fontFamilyNames {
print("------------------------------")
print("Font Family Name = [\(familyName)]")
let names = UIFont.fontNamesForFamilyName(familyName)
print("Font Names = [\(names)]")
}
}
class func attributedBodyText(string: String, color: UIColor) -> NSAttributedString {
let font = FontUtil.uiFont(AppUIFonts.TopicBody)
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = 23 - font.pointSize
paragraphStyle.alignment = .Center
let textAttributes: [String : AnyObject] = [NSFontAttributeName: font,
NSForegroundColorAttributeName: color,
NSKernAttributeName: font.getBodyKernAttributeValueForStandardTracking(),
NSParagraphStyleAttributeName: paragraphStyle]
let attributedString = NSAttributedString.init(string: string, attributes: textAttributes)
return attributedString
}
class func attributedLeagueName(string: String, color: UIColor) -> NSAttributedString {
let font = FontUtil.uiFont(AppUIFonts.LeagueName)
let textAttributes: [String : AnyObject] = [NSFontAttributeName: font,
NSForegroundColorAttributeName: color,
NSKernAttributeName: font.getBodyKernAttributeValueForStandardTracking()]
let attributedString = NSAttributedString.init(string: string, attributes: textAttributes)
return attributedString
}
class func attributedLeagueDirectoryName(string: String, color: UIColor) -> NSAttributedString {
let font = FontUtil.uiFont(AppUIFonts.LeagueDirectoryName)
let textAttributes: [String : AnyObject] = [NSFontAttributeName: font,
NSForegroundColorAttributeName: color,
NSKernAttributeName: font.getBodyKernAttributeValueForStandardTracking()]
let attributedString = NSAttributedString.init(string: string, attributes: textAttributes)
return attributedString
}
class func attributedOnboardingQuestion(string: String, color: UIColor) -> NSAttributedString {
let font = FontUtil.uiFont(AppUIFonts.LeagueName)
let textAttributes: [String : AnyObject] = [NSFontAttributeName: font,
NSForegroundColorAttributeName: color,
NSKernAttributeName: font.getBodyKernAttributeValueForStandardTracking()]
let attributedString = NSAttributedString.init(string: string, attributes: textAttributes)
return attributedString
}
class func attributedStatHeader(string: String, color: UIColor) -> NSAttributedString {
let font = FontUtil.uiFont(AppUIFonts.UserStatHeading)
let textAttributes: [String : AnyObject] = [NSFontAttributeName: font,
NSForegroundColorAttributeName: color,
NSKernAttributeName: font.getKernAttributeValueForStandardTracking()]
let attributedString = NSAttributedString.init(string: string.uppercaseString, attributes: textAttributes)
return attributedString
}
class func attributedStatBoldHeader(string: String, color: UIColor) -> NSAttributedString {
let font = FontUtil.uiFont(AppUIFonts.UserStatHeadingBold)
let textAttributes: [String : AnyObject] = [NSFontAttributeName: font,
NSForegroundColorAttributeName: color,
NSKernAttributeName: font.getKernAttributeValueForStandardTracking()]
let attributedString = NSAttributedString.init(string: string.uppercaseString, attributes: textAttributes)
return attributedString
}
class func attributedStatValue(string: String, color: UIColor) -> NSAttributedString {
let font = FontUtil.uiFont(AppUIFonts.UserStatValue)
let textAttributes: [String : AnyObject] = [NSFontAttributeName: font,
NSForegroundColorAttributeName: color,
NSKernAttributeName: font.getBodyKernAttributeValueForStandardTracking()]
let attributedString = NSAttributedString.init(string: string.uppercaseString, attributes: textAttributes)
return attributedString
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment