Commit 2ec72ce8 authored by ueshiba's avatar ueshiba
Browse files

- New Color and Font extensions

- Text and View Styles to apply directly into the UIView classes
- Textfield extension to add the placeholderColor property
- UILabel extension to apply the line height property
- UIImage extension to create a new image from a color
parent e92d2637
......@@ -251,6 +251,12 @@
841BE612212AFFA7002EF2D1 /* GiftCardUtilTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 841BE611212AFFA7002EF2D1 /* GiftCardUtilTests.swift */; };
843C67C22122E714005A3FDA /* AccountInfoTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 843C67C12122E714005A3FDA /* AccountInfoTests.swift */; };
843C67C32122EA13005A3FDA /* AccountInfoTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 843C67C12122E714005A3FDA /* AccountInfoTests.swift */; };
84577FBB213D8ED1006DEC3D /* StyleGuideHelpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84577FBA213D8ED1006DEC3D /* StyleGuideHelpers.swift */; };
84577FBD213D8EDD006DEC3D /* TextStyles.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84577FBC213D8EDD006DEC3D /* TextStyles.swift */; };
84577FBF213D8EE6006DEC3D /* ViewStyles.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84577FBE213D8EE6006DEC3D /* ViewStyles.swift */; };
84577FC1213D9A23006DEC3D /* UIImage+Color.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84577FC0213D9A23006DEC3D /* UIImage+Color.swift */; };
84577FC3213D9AEA006DEC3D /* UITextField+PlaceholderColor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84577FC2213D9AEA006DEC3D /* UITextField+PlaceholderColor.swift */; };
84577FC5213D9B4D006DEC3D /* UILabel+LineHeight.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84577FC4213D9B4D006DEC3D /* UILabel+LineHeight.swift */; };
84D5DA702126CE2900F753F8 /* QRCameraScannerViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84D5DA6F2126CE2900F753F8 /* QRCameraScannerViewController.swift */; };
9C43A37DD530B9DDB7C89A31 /* Pods_PIALibrary_PIALibraryTests_iOS.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = A3625593159B612C0128DC56 /* Pods_PIALibrary_PIALibraryTests_iOS.framework */; };
/* End PBXBuildFile section */
......@@ -494,6 +500,12 @@
841BE60E212AFE49002EF2D1 /* GiftCardUtil.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GiftCardUtil.swift; sourceTree = "<group>"; };
841BE611212AFFA7002EF2D1 /* GiftCardUtilTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GiftCardUtilTests.swift; sourceTree = "<group>"; };
843C67C12122E714005A3FDA /* AccountInfoTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AccountInfoTests.swift; sourceTree = "<group>"; };
84577FBA213D8ED1006DEC3D /* StyleGuideHelpers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StyleGuideHelpers.swift; sourceTree = "<group>"; };
84577FBC213D8EDD006DEC3D /* TextStyles.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TextStyles.swift; sourceTree = "<group>"; };
84577FBE213D8EE6006DEC3D /* ViewStyles.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewStyles.swift; sourceTree = "<group>"; };
84577FC0213D9A23006DEC3D /* UIImage+Color.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIImage+Color.swift"; sourceTree = "<group>"; };
84577FC2213D9AEA006DEC3D /* UITextField+PlaceholderColor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UITextField+PlaceholderColor.swift"; sourceTree = "<group>"; };
84577FC4213D9B4D006DEC3D /* UILabel+LineHeight.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UILabel+LineHeight.swift"; sourceTree = "<group>"; };
84D5DA6F2126CE2900F753F8 /* QRCameraScannerViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = QRCameraScannerViewController.swift; sourceTree = "<group>"; };
8AC8D338EE518EF3D7D996C5 /* Pods_PIALibrary_PIALibraryHost_iOS.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_PIALibrary_PIALibraryHost_iOS.framework; sourceTree = BUILT_PRODUCTS_DIR; };
A3625593159B612C0128DC56 /* Pods_PIALibrary_PIALibraryTests_iOS.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_PIALibrary_PIALibraryTests_iOS.framework; sourceTree = BUILT_PRODUCTS_DIR; };
......@@ -785,6 +797,10 @@
0EB8C05C1F9CD38A005857E4 /* Theme.swift */,
0E5BCBFE20172C8300E3E4B4 /* Theme+LightPalette.swift */,
0EB8C05B1F9CD38A005857E4 /* WelcomePageViewController.swift */,
84577FC0213D9A23006DEC3D /* UIImage+Color.swift */,
84577FC2213D9AEA006DEC3D /* UITextField+PlaceholderColor.swift */,
84577FC4213D9B4D006DEC3D /* UILabel+LineHeight.swift */,
84577FB9213D8EB2006DEC3D /* Styles */,
);
path = iOS;
sourceTree = "<group>";
......@@ -1007,6 +1023,16 @@
name = Frameworks;
sourceTree = "<group>";
};
84577FB9213D8EB2006DEC3D /* Styles */ = {
isa = PBXGroup;
children = (
84577FBA213D8ED1006DEC3D /* StyleGuideHelpers.swift */,
84577FBC213D8EDD006DEC3D /* TextStyles.swift */,
84577FBE213D8EE6006DEC3D /* ViewStyles.swift */,
);
path = Styles;
sourceTree = "<group>";
};
/* End PBXGroup section */
/* Begin PBXHeadersBuildPhase section */
......@@ -1466,12 +1492,14 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
84577FBD213D8EDD006DEC3D /* TextStyles.swift in Sources */,
0E3D13CD1F9DDD2400434A48 /* NotificationKey.swift in Sources */,
0EB8C06B1F9CD38B005857E4 /* RestoreSignupViewController.swift in Sources */,
0EAA38931F9CC682000149CF /* SignupRequest.swift in Sources */,
0E392DB01FE3366B0002160D /* NSData+Compression.m in Sources */,
0E9D62891FDE9C00009A90CF /* Data+Crypto.swift in Sources */,
0E53A84D1FE5BB25000C2A18 /* VPNDaemon.swift in Sources */,
84577FBB213D8ED1006DEC3D /* StyleGuideHelpers.swift in Sources */,
0EB8C0701F9CD38B005857E4 /* WelcomePageViewController.swift in Sources */,
0ED1585C1FDC0D1B008F6522 /* SwiftGen+Storyboards.swift in Sources */,
0EB8C0651F9CD38B005857E4 /* BorderedTextField.swift in Sources */,
......@@ -1482,6 +1510,7 @@
0EE78AF81F818815002E4CDD /* AccountInfo.swift in Sources */,
0EFB512A1F82D45F0033B81F /* DefaultAccountProvider.swift in Sources */,
0E3D13D91F9E273300434A48 /* GlossSignup.swift in Sources */,
84577FBF213D8EE6006DEC3D /* ViewStyles.swift in Sources */,
0E0E5B0F1F8297C500022CD0 /* SecureStore.swift in Sources */,
0EAA388B1F9CC4C4000149CF /* InAppProduct.swift in Sources */,
0EAA388D1F9CC4C4000149CF /* InAppTransaction.swift in Sources */,
......@@ -1516,6 +1545,7 @@
0EB8C0691F9CD38B005857E4 /* PurchasePlanCell.swift in Sources */,
0EA8072F20A1E7C60033EC1A /* GlossRedeem.swift in Sources */,
0EA4C4311FDDD48F0041C3D8 /* Server.swift in Sources */,
84577FC5213D9B4D006DEC3D /* UILabel+LineHeight.swift in Sources */,
0EB9667E1FDF36490086ABC2 /* GlossParser.swift in Sources */,
0EAA38991F9CC7E4000149CF /* AppStoreTransaction.swift in Sources */,
0EA8072720A1A0090033EC1A /* Redeem.swift in Sources */,
......@@ -1550,9 +1580,11 @@
0E3D13D31F9E270A00434A48 /* GlossAccountInfo.swift in Sources */,
0E245C9D1FECF0C20010DEF2 /* ClientAccess.swift in Sources */,
0EF14E4B1FEAE6350007485A /* Client+Providers.swift in Sources */,
84577FC1213D9A23006DEC3D /* UIImage+Color.swift in Sources */,
0E38E4101FF81722008223AB /* Notification+UI.swift in Sources */,
0E392DA91FE329AD0002160D /* PlatformVPNLog.swift in Sources */,
0EB8C0481F9CCE07005857E4 /* Macros.swift in Sources */,
84577FC3213D9AEA006DEC3D /* UITextField+PlaceholderColor.swift in Sources */,
0EFEB4C12007784A00F81029 /* PIATunnelProvider+Profile.swift in Sources */,
0E392DA31FE3247E0002160D /* Endpoint.swift in Sources */,
0E53A8581FE5DA16000C2A18 /* MockInAppProvider.swift in Sources */,
......
//
// PIAColors.swift
// PIALibrary-iOS
//
// Created by Jose Antonio Blaya Garcia on 3/9/18.
// Copyright © 2018 London Trust Media. All rights reserved.
//
import Foundation
import UIKit
extension UIColor {
class var grey15: UIColor {
return UIColor(red: 34.0 / 255.0, green: 37.0 / 255.0, blue: 46.0 / 255.0, alpha: 1.0)
}
class var grey18: UIColor {
return UIColor(red: 43.0 / 255.0, green: 46.0 / 255.0, blue: 57.0 / 255.0, alpha: 1.0)
}
class var grey20: UIColor {
return UIColor(red: 50.0 / 255.0, green: 54.0 / 255.0, blue: 66.0 / 255.0, alpha: 1.0)
}
class var grey40: UIColor {
return UIColor(red: 92.0 / 255.0, green: 99.0 / 255.0, blue: 112.0 / 255.0, alpha: 1.0)
}
class var grey55: UIColor {
return UIColor(red: 136.0 / 255.0, green: 144.0 / 255.0, blue: 153.0 / 255.0, alpha: 1.0)
}
class var grey85: UIColor {
return UIColor(red: 215.0 / 255.0, green: 216.0 / 255.0, blue: 217.0 / 255.0, alpha: 1.0)
}
class var white: UIColor {
return UIColor(white: 1.0, alpha: 1.0)
}
class var grey92: UIColor {
return UIColor(white: 238.0 / 255.0, alpha: 1.0)
}
class var green: UIColor {
return UIColor(red: 93.0 / 255.0, green: 223.0 / 255.0, blue: 90.0 / 255.0, alpha: 1.0)
}
class var greenDark20: UIColor {
return UIColor(red: 76.0 / 255.0, green: 182.0 / 255.0, blue: 73.0 / 255.0, alpha: 1.0)
}
class var greenDark50: UIColor {
return UIColor(red: 3.0 / 255.0, green: 121.0 / 255.0, blue: 0.0, alpha: 1.0)
}
class var yellow: UIColor {
return UIColor(red: 249.0 / 255.0, green: 207.0 / 255.0, blue: 1.0 / 255.0, alpha: 1.0)
}
class var yellowDark: UIColor {
return UIColor(red: 230.0 / 255.0, green: 180.0 / 255.0, blue: 0.0, alpha: 1.0)
}
class var red: UIColor {
return UIColor(red: 242.0 / 255.0, green: 68.0 / 255.0, blue: 88.0 / 255.0, alpha: 1.0)
}
class var redDark: UIColor {
return UIColor(red: 178.0 / 255.0, green: 53.0 / 255.0, blue: 45.0 / 255.0, alpha: 1.0)
}
}
//
// PIAFonts.swift
// PIALibrary
//
// Created by Jose Antonio Blaya Garcia on 3/9/18.
// Copyright © 2018 London Trust Media. All rights reserved.
//
import UIKit
private enum Fonts {
static let robotoTextRegular = "Roboto-Regular"
}
extension UIFont {
public class func regularFontWith(size: CGFloat) -> UIFont! {
return UIFont(name: Fonts.robotoTextRegular, size: size)
}
}
//
// StyleGuideHelpers.swift
// PIALibrary-iOS
//
// Created by Jose Antonio Blaya Garcia on 3/9/18.
// Copyright © 2018 London Trust Media. All rights reserved.
//
import UIKit
// MARK: Structs
public struct TextStyle {
public let font: UIFont?
public let color: UIColor?
public let foregroundColor: UIColor?
public let backgroundColor: UIColor?
public let tintColor: UIColor?
public let lineHeight: CGFloat?
public init(font: UIFont?,
color: UIColor?,
foregroundColor: UIColor?,
backgroundColor: UIColor?,
tintColor: UIColor?,
lineHeight: CGFloat? = 0) {
self.font = font
self.color = color
self.foregroundColor = foregroundColor
self.backgroundColor = backgroundColor
self.tintColor = tintColor
self.lineHeight = lineHeight
}
}
public struct ViewStyle {
let backgroundColor: UIColor?
let tintColor: UIColor?
let layerStyle: LayerStyle?
public init(backgroundColor: UIColor?, tintColor: UIColor?, layerStyle: LayerStyle?) {
self.backgroundColor = backgroundColor
self.tintColor = tintColor
self.layerStyle = layerStyle
}
public struct LayerStyle {
let masksToBounds: Bool?
let cornerRadius: CGFloat?
let borderStyle: BorderStyle?
let shadowStyle: ShadowStyle?
public init(masksToBounds: Bool?, cornerRadius: CGFloat?, borderStyle: BorderStyle?, shadowStyle: ShadowStyle?) {
self.masksToBounds = masksToBounds
self.cornerRadius = cornerRadius
self.borderStyle = borderStyle
self.shadowStyle = shadowStyle
}
public struct BorderStyle {
let color: UIColor
let width: CGFloat
public init(color: UIColor, width: CGFloat) {
self.width = width
self.color = color
}
}
public struct ShadowStyle {
let color: UIColor
let radius: CGFloat
let offset: CGSize
let opacity: Float
public init(color: UIColor, radius: CGFloat, offset: CGSize, opacity: Float) {
self.radius = radius
self.color = color
self.offset = offset
self.opacity = opacity
}
}
}
}
public struct TextAttributeStyle {
let kern: CGFloat?
let paragraphStyle: NSMutableParagraphStyle?
let foregroundColor: UIColor?
let font: UIFont?
let link: String?
let baselineOffset: CGFloat?
public init(kern: CGFloat?, paragraphStyle: NSMutableParagraphStyle?, foregroundColor: UIColor?,
font: UIFont?, link: String?, baselineOffset: CGFloat?) {
self.kern = kern
self.paragraphStyle = paragraphStyle
self.foregroundColor = foregroundColor
self.font = font
self.link = link
self.baselineOffset = baselineOffset
}
func toAttributeArray() -> [NSAttributedStringKey: Any] {
var array = [NSAttributedStringKey: Any]()
array[NSAttributedStringKey.kern] = kern
array[NSAttributedStringKey.paragraphStyle] = paragraphStyle
array[NSAttributedStringKey.foregroundColor] = foregroundColor
array[NSAttributedStringKey.font] = font
array[NSAttributedStringKey.link] = link
array[NSAttributedStringKey.baselineOffset] = baselineOffset
return array
}
}
// MARK: Helper extensions
extension UIView: ViewStyling {
public func style(style: ViewStyle) {
if let backgroundColor = style.backgroundColor {
self.backgroundColor = backgroundColor
}
if let tintColor = style.tintColor {
self.tintColor = tintColor
}
if let layerStyle = style.layerStyle {
if let cornerRadius = layerStyle.cornerRadius {
self.layer.cornerRadius = cornerRadius
}
if let masksToBounds = layerStyle.masksToBounds {
self.layer.masksToBounds = masksToBounds
}
if let borderStyle = layerStyle.borderStyle {
self.layer.borderColor = borderStyle.color.cgColor
self.layer.borderWidth = borderStyle.width
}
if let shadowStyle = layerStyle.shadowStyle {
self.layer.applySketchShadow(color: shadowStyle.color,
alpha: shadowStyle.opacity,
offSet: shadowStyle.offset,
radius: shadowStyle.radius)
}
}
}
}
extension CALayer {
func applySketchShadow(
color: UIColor = .black,
alpha: Float = 0.12,
offSet: CGSize = CGSize.zero,
blur: CGFloat = 6,
radius: CGFloat = 2.0,
spread: CGFloat = 0)
{
shadowColor = color.cgColor
shadowOpacity = alpha
shadowOffset = offSet
shadowRadius = blur / 2.0
if spread == 0 {
shadowPath = nil
} else {
let dx = -spread
let rect = bounds.insetBy(dx: dx, dy: dx)
shadowPath = UIBezierPath(rect: rect).cgPath
}
}
}
extension UILabel: TextStyling {
public func style(style: TextStyle) {
font = style.font
textColor = style.color
if let lineHeight = style.lineHeight {
setLineHeight(lineHeight)
}
}
}
extension UIButton: TextStyling {
public func style(style: TextStyle) {
titleLabel?.font = style.font
setTitleColor(style.color, for: .normal)
backgroundColor = style.backgroundColor
}
public func style(style: TextStyle, for controlState: UIControlState) {
titleLabel?.font = style.font
setTitleColor(style.color, for: controlState)
if let color = style.backgroundColor {
backgroundColor = nil
setBackgroundImage(UIImage.fromColor(color), for: controlState)
} else {
backgroundColor = style.backgroundColor
}
}
}
extension UITextField: TextStyling {
public func style(style: TextStyle) {
font = style.font
textColor = style.color
placeholderColor = style.foregroundColor
tintColor = style.tintColor
}
}
extension UITextView: TextStyling {
public func style(style: TextStyle) {
font = style.font
textColor = style.color
}
}
//
// TextStyles.swift
// PIALibrary-iOS
//
// Created by Jose Antonio Blaya Garcia on 3/9/18.
// Copyright © 2018 London Trust Media. All rights reserved.
//
import UIKit
/// MARK: Helper protocols
protocol TextStyling {
func style(style: TextStyle)
}
public extension TextStyle {
public static let textStyle1 = TextStyle(
font: UIFont.regularFontWith(size: 14),
color: UIColor.white,
foregroundColor: nil,
backgroundColor: nil,
tintColor: nil,
lineHeight: 18
)
public static let textStyle2 = TextStyle(
font: UIFont.regularFontWith(size: 14),
color: UIColor.grey55,
foregroundColor: nil,
backgroundColor: nil,
tintColor: nil,
lineHeight: 18
)
public static let textStyle3 = TextStyle(
font: UIFont.regularFontWith(size: 14),
color: UIColor.greenDark20,
foregroundColor: nil,
backgroundColor: nil,
tintColor: nil,
lineHeight: 18
)
public static let textStyle4 = TextStyle(
font: UIFont.regularFontWith(size: 14),
color: UIColor.red,
foregroundColor: nil,
backgroundColor: nil,
tintColor: nil,
lineHeight: 18
)
public static let textStyle5 = TextStyle(
font: UIFont.regularFontWith(size: 14),
color: UIColor.grey20,
foregroundColor: nil,
backgroundColor: nil,
tintColor: nil,
lineHeight: 18
)
public static let textStyle6 = TextStyle(
font: UIFont.regularFontWith(size: 12),
color: UIColor.white,
foregroundColor: nil,
backgroundColor: nil,
tintColor: nil,
lineHeight: 16
)
public static let textStyle7 = TextStyle(
font: UIFont.regularFontWith(size: 12),
color: UIColor.grey55,
foregroundColor: nil,
backgroundColor: nil,
tintColor: nil,
lineHeight: 16
)
public static let textStyle8 = TextStyle(
font: UIFont.regularFontWith(size: 12),
color: UIColor.greenDark20,
foregroundColor: nil,
backgroundColor: nil,
tintColor: nil,
lineHeight: 16
)
public static let textStyle9 = TextStyle(
font: UIFont.regularFontWith(size: 12),
color: UIColor.red,
foregroundColor: nil,
backgroundColor: nil,
tintColor: nil,
lineHeight: 16
)
public static let textStyle10 = TextStyle(
font: UIFont.regularFontWith(size: 12),
color: UIColor.grey20,
foregroundColor: nil,
backgroundColor: nil,
tintColor: nil,
lineHeight: 16
)
public static let textStyle11 = TextStyle(
font: UIFont.regularFontWith(size: 10),
color: UIColor.white,
foregroundColor: nil,
backgroundColor: nil,
tintColor: nil,
lineHeight: 15
)
public static let textStyle12 = TextStyle(
font: UIFont.regularFontWith(size: 10),
color: UIColor.grey55,
foregroundColor: nil,
backgroundColor: nil,
tintColor: nil,
lineHeight: 15
)
public static let textStyle13 = TextStyle(
font: UIFont.regularFontWith(size: 10),
color: UIColor.greenDark20,
foregroundColor: nil,
backgroundColor: nil,
tintColor: nil,
lineHeight: 15
)
public static let textStyle14 = TextStyle(
font: UIFont.regularFontWith(size: 10),
color: UIColor.red,
foregroundColor: nil,
backgroundColor: nil,
tintColor: nil,
lineHeight: 15
)
public static let textStyle15 = TextStyle(
font: UIFont.regularFontWith(size: 10),
color: UIColor.grey20,
foregroundColor: nil,
backgroundColor: nil,
tintColor: nil,
lineHeight: 15
)
}