Skip to content

Commit dee70f1

Browse files
committed
Updated for Swift 3
1 parent b4d76c7 commit dee70f1

File tree

3 files changed

+36
-30
lines changed

3 files changed

+36
-30
lines changed

DynamicFontSizeExample.xcodeproj/project.pbxproj

+6
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@
9999
TargetAttributes = {
100100
29F2024E1CA13E4E00AA0746 = {
101101
CreatedOnToolsVersion = 7.3;
102+
DevelopmentTeam = MG8YZ6JLG6;
103+
LastSwiftMigration = 0800;
102104
};
103105
};
104106
};
@@ -255,21 +257,25 @@
255257
isa = XCBuildConfiguration;
256258
buildSettings = {
257259
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
260+
DEVELOPMENT_TEAM = MG8YZ6JLG6;
258261
INFOPLIST_FILE = DynamicFontSizeExample/Info.plist;
259262
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
260263
PRODUCT_BUNDLE_IDENTIFIER = com.Uthoft.DynamicFontSizeExample;
261264
PRODUCT_NAME = "$(TARGET_NAME)";
265+
SWIFT_VERSION = 3.0;
262266
};
263267
name = Debug;
264268
};
265269
29F202631CA13E4E00AA0746 /* Release */ = {
266270
isa = XCBuildConfiguration;
267271
buildSettings = {
268272
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
273+
DEVELOPMENT_TEAM = MG8YZ6JLG6;
269274
INFOPLIST_FILE = DynamicFontSizeExample/Info.plist;
270275
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
271276
PRODUCT_BUNDLE_IDENTIFIER = com.Uthoft.DynamicFontSizeExample;
272277
PRODUCT_NAME = "$(TARGET_NAME)";
278+
SWIFT_VERSION = 3.0;
273279
};
274280
name = Release;
275281
};

DynamicFontSizeExample/AppDelegate.swift

+6-6
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,30 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
1414
var window: UIWindow?
1515

1616

17-
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
17+
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
1818
// Override point for customization after application launch.
1919
return true
2020
}
2121

22-
func applicationWillResignActive(application: UIApplication) {
22+
func applicationWillResignActive(_ application: UIApplication) {
2323
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
2424
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
2525
}
2626

27-
func applicationDidEnterBackground(application: UIApplication) {
27+
func applicationDidEnterBackground(_ application: UIApplication) {
2828
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
2929
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
3030
}
3131

32-
func applicationWillEnterForeground(application: UIApplication) {
32+
func applicationWillEnterForeground(_ application: UIApplication) {
3333
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
3434
}
3535

36-
func applicationDidBecomeActive(application: UIApplication) {
36+
func applicationDidBecomeActive(_ application: UIApplication) {
3737
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
3838
}
3939

40-
func applicationWillTerminate(application: UIApplication) {
40+
func applicationWillTerminate(_ application: UIApplication) {
4141
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
4242
}
4343

DynamicFontSizeHelper.swift

+24-24
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
import UIKit
22

33
private var fontSizeMultiplier : CGFloat {
4-
switch UIApplication.sharedApplication().preferredContentSizeCategory {
5-
case UIContentSizeCategoryAccessibilityExtraExtraExtraLarge: return 23 / 16
6-
case UIContentSizeCategoryAccessibilityExtraExtraLarge: return 22 / 16
7-
case UIContentSizeCategoryAccessibilityExtraLarge: return 21 / 16
8-
case UIContentSizeCategoryAccessibilityLarge: return 20 / 16
9-
case UIContentSizeCategoryAccessibilityMedium: return 19 / 16
10-
case UIContentSizeCategoryExtraExtraExtraLarge: return 19 / 16
11-
case UIContentSizeCategoryExtraExtraLarge: return 18 / 16
12-
case UIContentSizeCategoryExtraLarge: return 17 / 16
13-
case UIContentSizeCategoryLarge: return 1.0
14-
case UIContentSizeCategoryMedium: return 15 / 16
15-
case UIContentSizeCategorySmall: return 14 / 16
16-
case UIContentSizeCategoryExtraSmall: return 13 / 16
4+
switch UIApplication.shared.preferredContentSizeCategory {
5+
case UIContentSizeCategory.accessibilityExtraExtraExtraLarge: return 23 / 16
6+
case UIContentSizeCategory.accessibilityExtraExtraLarge: return 22 / 16
7+
case UIContentSizeCategory.accessibilityExtraLarge: return 21 / 16
8+
case UIContentSizeCategory.accessibilityLarge: return 20 / 16
9+
case UIContentSizeCategory.accessibilityMedium: return 19 / 16
10+
case UIContentSizeCategory.extraExtraExtraLarge: return 19 / 16
11+
case UIContentSizeCategory.extraExtraLarge: return 18 / 16
12+
case UIContentSizeCategory.extraLarge: return 17 / 16
13+
case UIContentSizeCategory.large: return 1.0
14+
case UIContentSizeCategory.medium: return 15 / 16
15+
case UIContentSizeCategory.small: return 14 / 16
16+
case UIContentSizeCategory.extraSmall: return 13 / 16
1717
default: return 1.0
1818
}
1919
}
@@ -27,37 +27,37 @@ private class ContentSizeCategoryChangeManager {
2727
weak var object: AnyObject?
2828
var block: ContentSizeCategoryDidChangeCallback
2929

30-
init(object: AnyObject, block: ContentSizeCategoryDidChangeCallback) {
30+
init(object: AnyObject, block: @escaping ContentSizeCategoryDidChangeCallback) {
3131
self.object = object
3232
self.block = block
3333
}
3434
}
3535

36-
private var observerPool: [Observer] = []
36+
fileprivate var observerPool: [Observer] = []
3737

38-
private init() {
39-
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(contentSizeCategoryDidChange(_:)), name: UIContentSizeCategoryDidChangeNotification, object: nil)
38+
fileprivate init() {
39+
NotificationCenter.default.addObserver(self, selector: #selector(contentSizeCategoryDidChange(_:)), name: NSNotification.Name.UIContentSizeCategoryDidChange, object: nil)
4040
}
4141

42-
func addCallback(observer: AnyObject, block: ContentSizeCategoryDidChangeCallback) {
42+
func addCallback(_ observer: AnyObject, block: @escaping ContentSizeCategoryDidChangeCallback) {
4343
// Don't readd the call back.
44-
guard !observerPool.contains({ $0.object === observer }) else { return }
44+
guard !observerPool.contains(where: { $0.object === observer }) else { return }
4545

4646
// Run the block once to make sure the font size is initialized correctly.
4747
block()
4848

4949
observerPool.append(Observer(object: observer, block: block))
5050
}
5151

52-
@objc func contentSizeCategoryDidChange(notification: NSNotification) {
53-
dispatch_async(dispatch_get_main_queue()) { [unowned self] in
52+
@objc func contentSizeCategoryDidChange(_ notification: Notification) {
53+
DispatchQueue.main.async { [unowned self] in
5454
self.observerPool = self.observerPool.filter { $0.object != nil }
5555
self.observerPool.forEach { $0.block() }
5656
}
5757
}
5858

5959
deinit {
60-
NSNotificationCenter.defaultCenter().removeObserver(self)
60+
NotificationCenter.default.removeObserver(self)
6161
}
6262
}
6363

@@ -66,12 +66,12 @@ protocol FontSizeScalable: class {
6666
}
6767

6868
extension FontSizeScalable {
69-
private func registerForSizeChange(defaultFontSize: CGFloat? = nil) {
69+
fileprivate func registerForSizeChange(_ defaultFontSize: CGFloat? = nil) {
7070
let defaultFontSize = defaultFontSize ?? scalableFont.pointSize
7171

7272
ContentSizeCategoryChangeManager.sharedInstance.addCallback(self) { [weak self] _ in
7373
guard let _self = self else { return }
74-
_self.scalableFont = UIFont(descriptor: _self.scalableFont.fontDescriptor(), size: defaultFontSize * fontSizeMultiplier)
74+
_self.scalableFont = UIFont(descriptor: _self.scalableFont.fontDescriptor, size: defaultFontSize * fontSizeMultiplier)
7575
}
7676
}
7777
}

0 commit comments

Comments
 (0)