Skip to content

Commit ba8313e

Browse files
committed
Add some helpful extension functions that were being used in several projects.
- Add RSFontSizes dependency - Add RSFontSizes wrapper in UIFont extension - Add addNibView function to UIView extension - Add goToScreen function to UIViewController extension to instantiate view controller and/or storyboard and push or present modally in a few lines of code
1 parent 47e774e commit ba8313e

File tree

8 files changed

+178
-0
lines changed

8 files changed

+178
-0
lines changed

Podfile

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ inhibit_all_warnings!
55
target 'ios-base' do
66
pod 'Alamofire', '~> 4.7.3'
77
pod 'IQKeyboardManagerSwift', '~> 6.1.1'
8+
pod 'RSFontSizes', '1.0.2'
89

910
# FB SDK ---
1011
pod 'FBSDKCoreKit', '~> 4.33.0'

Podfile.lock

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
PODS:
2+
- Alamofire (4.7.3)
3+
- Bolts (1.9.0):
4+
- Bolts/AppLinks (= 1.9.0)
5+
- Bolts/Tasks (= 1.9.0)
6+
- Bolts/AppLinks (1.9.0):
7+
- Bolts/Tasks
8+
- Bolts/Tasks (1.9.0)
9+
- Device (3.1.2)
10+
- FBSDKCoreKit (4.33.0):
11+
- Bolts (~> 1.7)
12+
- FBSDKLoginKit (4.33.0):
13+
- FBSDKCoreKit
14+
- IQKeyboardManagerSwift (6.1.1)
15+
- KIF (3.7.4):
16+
- KIF/Core (= 3.7.4)
17+
- KIF/Core (3.7.4)
18+
- KIF/IdentifierTests (3.7.4):
19+
- KIF/Core
20+
- OHHTTPStubs/Core (6.1.0)
21+
- OHHTTPStubs/Default (6.1.0):
22+
- OHHTTPStubs/Core
23+
- OHHTTPStubs/JSON
24+
- OHHTTPStubs/NSURLSession
25+
- OHHTTPStubs/OHPathHelpers
26+
- OHHTTPStubs/JSON (6.1.0):
27+
- OHHTTPStubs/Core
28+
- OHHTTPStubs/NSURLSession (6.1.0):
29+
- OHHTTPStubs/Core
30+
- OHHTTPStubs/OHPathHelpers (6.1.0)
31+
- OHHTTPStubs/Swift (6.1.0):
32+
- OHHTTPStubs/Default
33+
- RSFontSizes (1.0.2):
34+
- Device (~> 3.1.2)
35+
36+
DEPENDENCIES:
37+
- Alamofire (~> 4.7.3)
38+
- FBSDKCoreKit (~> 4.33.0)
39+
- FBSDKLoginKit (~> 4.33.0)
40+
- IQKeyboardManagerSwift (~> 6.1.1)
41+
- KIF (~> 3.7.4)
42+
- KIF/IdentifierTests (~> 3.7.3)
43+
- OHHTTPStubs/Swift (~> 6.1.0)
44+
- RSFontSizes (= 1.0.2)
45+
46+
SPEC REPOS:
47+
https://github.com/cocoapods/specs.git:
48+
- Alamofire
49+
- Bolts
50+
- Device
51+
- FBSDKCoreKit
52+
- FBSDKLoginKit
53+
- IQKeyboardManagerSwift
54+
- KIF
55+
- OHHTTPStubs
56+
- RSFontSizes
57+
58+
SPEC CHECKSUMS:
59+
Alamofire: c7287b6e5d7da964a70935e5db17046b7fde6568
60+
Bolts: ac6567323eac61e203f6a9763667d0f711be34c8
61+
Device: 62242076214c30fb5760174b3601cefafa70a481
62+
FBSDKCoreKit: 572b047a7e029bc44542bcf8a59414e7ff2b543e
63+
FBSDKLoginKit: 88cb456349cfb3b554427ce4f8b43729d85dfb40
64+
IQKeyboardManagerSwift: 977affaeb4d6e971975c7790a6850f31d38f1207
65+
KIF: f3d01c109ee9d1cfe6b0f2bb2aa278e807e971e2
66+
OHHTTPStubs: 1e21c7d2c084b8153fc53d48400d8919d2d432d0
67+
RSFontSizes: cf14ae41c2807b66573f7064528ccff8c98251c9
68+
69+
PODFILE CHECKSUM: 611d5e2d103df726c4a2a805e7909a7ce08ad4fa
70+
71+
COCOAPODS: 1.5.3

ios-base.xcodeproj/project.pbxproj

+8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
071CD2622228544700E6D385 /* FontExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 071CD2612228544700E6D385 /* FontExtension.swift */; };
1011
07741D72218CDED600DB3B97 /* FirstViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 07741D71218CDED600DB3B97 /* FirstViewModel.swift */; };
1112
0A6990351F9F82A300F4BAC7 /* UIApplicationExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0A6990341F9F82A300F4BAC7 /* UIApplicationExtension.swift */; };
1213
0ABFB8901FA366D90098F751 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 0ABFB8921FA366D90098F751 /* Main.storyboard */; };
@@ -67,6 +68,7 @@
6768
/* End PBXContainerItemProxy section */
6869

6970
/* Begin PBXFileReference section */
71+
071CD2612228544700E6D385 /* FontExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FontExtension.swift; sourceTree = "<group>"; };
7072
07741D71218CDED600DB3B97 /* FirstViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FirstViewModel.swift; sourceTree = "<group>"; };
7173
0A6990341F9F82A300F4BAC7 /* UIApplicationExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIApplicationExtension.swift; sourceTree = "<group>"; };
7274
0ABFB8911FA366D90098F751 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; };
@@ -434,6 +436,7 @@
434436
0A6990341F9F82A300F4BAC7 /* UIApplicationExtension.swift */,
435437
9B8D306F20AB47E90050697F /* JSONEncodingExtension.swift */,
436438
9B8574D8212C84130063A3E2 /* UIImageExtension.swift */,
439+
071CD2612228544700E6D385 /* FontExtension.swift */,
437440
);
438441
path = Extensions;
439442
sourceTree = "<group>";
@@ -563,17 +566,21 @@
563566
"${SRCROOT}/Pods/Target Support Files/Pods-ios-base/Pods-ios-base-frameworks.sh",
564567
"${BUILT_PRODUCTS_DIR}/Alamofire/Alamofire.framework",
565568
"${BUILT_PRODUCTS_DIR}/Bolts/Bolts.framework",
569+
"${BUILT_PRODUCTS_DIR}/Device/Device.framework",
566570
"${BUILT_PRODUCTS_DIR}/FBSDKCoreKit/FBSDKCoreKit.framework",
567571
"${BUILT_PRODUCTS_DIR}/FBSDKLoginKit/FBSDKLoginKit.framework",
568572
"${BUILT_PRODUCTS_DIR}/IQKeyboardManagerSwift/IQKeyboardManagerSwift.framework",
573+
"${BUILT_PRODUCTS_DIR}/RSFontSizes/RSFontSizes.framework",
569574
);
570575
name = "[CP] Embed Pods Frameworks";
571576
outputPaths = (
572577
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/Alamofire.framework",
573578
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/Bolts.framework",
579+
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/Device.framework",
574580
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/FBSDKCoreKit.framework",
575581
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/FBSDKLoginKit.framework",
576582
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/IQKeyboardManagerSwift.framework",
583+
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/RSFontSizes.framework",
577584
);
578585
runOnlyForDeploymentPostprocessing = 0;
579586
shellPath = /bin/sh;
@@ -677,6 +684,7 @@
677684
9B0C72741C738D3400BAF3B1 /* APIClient.swift in Sources */,
678685
9BD01E361F01641E007255E3 /* DictionaryExtension.swift in Sources */,
679686
E8290C021D8330A800599960 /* StringExtension.swift in Sources */,
687+
071CD2622228544700E6D385 /* FontExtension.swift in Sources */,
680688
9B8D307020AB47E90050697F /* JSONEncodingExtension.swift in Sources */,
681689
E81171921DE5EFB7003D3DF5 /* UIViewControllerExtension.swift in Sources */,
682690
9B3AA3991ED35007005A4D26 /* SignInViewController.swift in Sources */,

ios-base.xcworkspace/contents.xcworkspacedata

+10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>IDEDidComputeMac32BitWarning</key>
6+
<true/>
7+
</dict>
8+
</plist>
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//
2+
// FontExtension.swift
3+
// ios-base
4+
//
5+
// Created by Germán Stábile on 2/28/19.
6+
// Copyright © 2019 TopTier labs. All rights reserved.
7+
//
8+
9+
import UIKit
10+
import RSFontSizes
11+
12+
extension UIFont {
13+
var bold: UIFont { return withWeight(.bold) }
14+
var semibold: UIFont { return withWeight(.semibold) }
15+
var light: UIFont { return withWeight(.ultraLight) }
16+
17+
private func withWeight(_ weight: UIFont.Weight) -> UIFont {
18+
var attributes = fontDescriptor.fontAttributes
19+
var traits = (attributes[.traits] as? [UIFontDescriptor.TraitKey: Any]) ?? [:]
20+
21+
traits[.weight] = weight
22+
23+
attributes[.name] = nil
24+
attributes[.traits] = traits
25+
attributes[.family] = familyName
26+
27+
let descriptor = UIFontDescriptor(fontAttributes: attributes)
28+
29+
return UIFont(descriptor: descriptor, size: pointSize)
30+
}
31+
32+
static func font(withName name: String, size: CGFloat) -> UIFont {
33+
let size = Font.PointSize.proportional(to: (.screen6_5Inch,
34+
size)).value()
35+
let font = UIFont(name: name,
36+
size: size)
37+
return font ?? UIFont.systemFont(ofSize: size)
38+
}
39+
}

ios-base/Extensions/UIViewControllerExtension.swift

+21
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,25 @@ extension UIViewController {
1616
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertAction.Style.default, handler: handler))
1717
present(alert, animated: true, completion: nil)
1818
}
19+
20+
func goToScreen(withIdentifier identifier: String,
21+
storyboardId: String? = nil,
22+
modally: Bool = false) {
23+
var storyboard = self.storyboard
24+
25+
if let storyboardId = storyboardId {
26+
storyboard = UIStoryboard(name: storyboardId, bundle: nil)
27+
}
28+
29+
guard let viewController =
30+
storyboard?.instantiateViewController(withIdentifier: identifier) else {
31+
return
32+
}
33+
34+
if modally {
35+
present(viewController, animated: true)
36+
} else {
37+
navigationController?.pushViewController(viewController, animated: true)
38+
}
39+
}
1940
}

ios-base/Extensions/UIViewExtension.swift

+20
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,26 @@ extension UIView {
2121
clipsToBounds = true
2222
layer.cornerRadius = cornerRadius
2323
}
24+
25+
var typeName: String {
26+
return String(describing: type(of: self))
27+
}
28+
29+
func instanceFromNib(withName name: String) -> UIView? {
30+
return UINib(nibName: name,
31+
bundle: nil).instantiate(withOwner: self,
32+
options: nil).first as? UIView
33+
}
34+
35+
func addNibView(withName nibName: String? = nil) -> UIView? {
36+
guard let view = instanceFromNib(withName: nibName ?? typeName)
37+
else { return nil }
38+
39+
view.frame = bounds
40+
view.autoresizingMask = [.flexibleHeight, .flexibleWidth]
41+
addSubview(view)
42+
return view
43+
}
2444
}
2545

2646
extension Array where Element: UIView {

0 commit comments

Comments
 (0)