From 9ed95b4760c3e8c8fd7a3e18cc64a64ca1557e17 Mon Sep 17 00:00:00 2001 From: zirinisp Date: Wed, 21 Sep 2016 15:52:13 +0300 Subject: [PATCH] Converted to Swift 3 --- ExampleApp/AppDelegate.swift | 2 +- ExampleApp/Base.lproj/Main.storyboard | 61 ++++++++++--------- ExampleApp/ViewController.swift | 10 +-- ExampleApp/WatcherViewController.swift | 12 ++-- Gliphy.xcodeproj/project.pbxproj | 25 ++++---- .../xcshareddata/xcschemes/Gliphy.xcscheme | 2 +- GliphyTests/FontRegistryTests.swift | 8 ++- GliphyTests/GliphyTests.swift | 8 +-- Sources/Core/DynamicFontRegistry.swift | 16 ++--- Sources/Core/DynamicTypeElement.swift | 7 ++- Sources/Core/DynamicTypeManager.swift | 32 +++++----- Sources/Core/Extensions.swift | 30 ++++++--- Sources/Core/NSMapTableExtensions.swift | 27 -------- Sources/Core/StyleConfig.swift | 2 +- Sources/Core/StyleWatcher.swift | 24 ++++---- 15 files changed, 129 insertions(+), 137 deletions(-) delete mode 100644 Sources/Core/NSMapTableExtensions.swift diff --git a/ExampleApp/AppDelegate.swift b/ExampleApp/AppDelegate.swift index 9ac093d..9fff473 100644 --- a/ExampleApp/AppDelegate.swift +++ b/ExampleApp/AppDelegate.swift @@ -14,7 +14,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? - func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { + private func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { // Override point for customization after application launch. ConfigSetup.setup() diff --git a/ExampleApp/Base.lproj/Main.storyboard b/ExampleApp/Base.lproj/Main.storyboard index 952e719..d892f1c 100644 --- a/ExampleApp/Base.lproj/Main.storyboard +++ b/ExampleApp/Base.lproj/Main.storyboard @@ -1,9 +1,10 @@ - - + + - + + @@ -15,31 +16,31 @@ - + - - - + @@ -69,25 +70,25 @@ - + - - + @@ -151,7 +152,7 @@ - + diff --git a/ExampleApp/ViewController.swift b/ExampleApp/ViewController.swift index bfe0f19..fa54705 100644 --- a/ExampleApp/ViewController.swift +++ b/ExampleApp/ViewController.swift @@ -18,13 +18,13 @@ class ViewController: UIViewController { super.viewDidLoad() let style = "ReallyReallyBigFont" - DynamicFontRegistry.registry.addTextStyle(style, - scaledFrom: UIFontTextStyleHeadline, + DynamicFontRegistry.registry.addTextStyle(UIFontTextStyle(rawValue: style), + scaledFrom: UIFontTextStyle.headline, byFactor: 4) - Gliphy.sharedInstance.watchLabel(heading, textStyle: UIFontTextStyleHeadline, fontName: "Georgia") - Gliphy.sharedInstance.watchButton(tapme, textStyle: UIFontTextStyleBody, fontName: "Georgia") - Gliphy.sharedInstance.watchLabel(customStyleLabel, textStyle: "ReallyReallyBigFont", fontName: "Helvetica") + Gliphy.sharedInstance.watchLabel(heading, textStyle: UIFontTextStyle.headline, fontName: "Georgia") + Gliphy.sharedInstance.watchButton(tapme, textStyle: UIFontTextStyle.body, fontName: "Georgia") + Gliphy.sharedInstance.watchLabel(customStyleLabel, textStyle: UIFontTextStyle(rawValue: "ReallyReallyBigFont"), fontName: "Helvetica") } } diff --git a/ExampleApp/WatcherViewController.swift b/ExampleApp/WatcherViewController.swift index 3468ca6..5bc3182 100644 --- a/ExampleApp/WatcherViewController.swift +++ b/ExampleApp/WatcherViewController.swift @@ -11,11 +11,11 @@ import UIKit struct ConfigSetup { static func setup() { var config = StyleConfig() - config.label[UIFontTextStyleHeadline] = "Verdana" - config.label[UIFontTextStyleCaption1] = "MarkerFelt-Thin" - config.button[UIFontTextStyleHeadline] = "Verdana" - config.textField[UIFontTextStyleBody] = "Verdana" - config.textField[UIFontTextStyleCaption1] = "Helvetica" + config.label[UIFontTextStyle.headline] = "Verdana" + config.label[UIFontTextStyle.caption1] = "MarkerFelt-Thin" + config.button[UIFontTextStyle.headline] = "Verdana" + config.textField[UIFontTextStyle.body] = "Verdana" + config.textField[UIFontTextStyle.caption1] = "Helvetica" StyleWatcher.defaultConfig = config } } @@ -29,7 +29,7 @@ class WatcherViewController: UIViewController, UITextFieldDelegate { watcher.watchViews(inView: view) } - func textFieldShouldReturn(textField: UITextField) -> Bool { + func textFieldShouldReturn(_ textField: UITextField) -> Bool { textField.resignFirstResponder() return true } diff --git a/Gliphy.xcodeproj/project.pbxproj b/Gliphy.xcodeproj/project.pbxproj index 0a84bb9..afc0dfc 100644 --- a/Gliphy.xcodeproj/project.pbxproj +++ b/Gliphy.xcodeproj/project.pbxproj @@ -7,6 +7,7 @@ objects = { /* Begin PBXBuildFile section */ + 646FEAE51D92B89600375EB7 /* DynamicTypeElement.swift in Sources */ = {isa = PBXBuildFile; fileRef = A784B2371C741BED004EB35B /* DynamicTypeElement.swift */; }; A779B0881C8F67B50044996A /* DynamicFontRegistry.swift in Sources */ = {isa = PBXBuildFile; fileRef = A779B0871C8F67B50044996A /* DynamicFontRegistry.swift */; }; A779B0891C8F67B50044996A /* DynamicFontRegistry.swift in Sources */ = {isa = PBXBuildFile; fileRef = A779B0871C8F67B50044996A /* DynamicFontRegistry.swift */; }; A779B08A1C8F67B50044996A /* DynamicFontRegistry.swift in Sources */ = {isa = PBXBuildFile; fileRef = A779B0871C8F67B50044996A /* DynamicFontRegistry.swift */; }; @@ -17,9 +18,6 @@ A784B2351C7417B3004EB35B /* DynamicTypeManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = A784B2341C7417B3004EB35B /* DynamicTypeManager.swift */; }; A784B2361C7417B3004EB35B /* DynamicTypeManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = A784B2341C7417B3004EB35B /* DynamicTypeManager.swift */; }; A784B2381C741BED004EB35B /* DynamicTypeElement.swift in Sources */ = {isa = PBXBuildFile; fileRef = A784B2371C741BED004EB35B /* DynamicTypeElement.swift */; }; - A784B2391C741BED004EB35B /* DynamicTypeElement.swift in Sources */ = {isa = PBXBuildFile; fileRef = A784B2371C741BED004EB35B /* DynamicTypeElement.swift */; }; - A784B23B1C741E84004EB35B /* NSMapTableExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = A784B23A1C741E84004EB35B /* NSMapTableExtensions.swift */; }; - A784B23C1C741E84004EB35B /* NSMapTableExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = A784B23A1C741E84004EB35B /* NSMapTableExtensions.swift */; }; A784B2451C743306004EB35B /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = A784B2441C743306004EB35B /* AppDelegate.swift */; }; A784B2471C743306004EB35B /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A784B2461C743306004EB35B /* ViewController.swift */; }; A784B24A1C743306004EB35B /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = A784B2481C743306004EB35B /* Main.storyboard */; }; @@ -36,7 +34,6 @@ A7EAD5711CD5BD6E008781F6 /* StyleConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = A79DBE9A1CB99AF8000BAA63 /* StyleConfig.swift */; }; A7EAD5721CD5BD6E008781F6 /* Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = A7EAD56B1CD5B0D0008781F6 /* Extensions.swift */; }; A7EAD5731CD5BD6E008781F6 /* DynamicTypeElement.swift in Sources */ = {isa = PBXBuildFile; fileRef = A784B2371C741BED004EB35B /* DynamicTypeElement.swift */; }; - A7EAD5741CD5BD6E008781F6 /* NSMapTableExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = A784B23A1C741E84004EB35B /* NSMapTableExtensions.swift */; }; A7EAD5751CD5BD6E008781F6 /* Util.swift in Sources */ = {isa = PBXBuildFile; fileRef = A7EAD5681CD5AFFE008781F6 /* Util.swift */; }; A7EAD5771CD5C679008781F6 /* StyleWatcher.swift in Sources */ = {isa = PBXBuildFile; fileRef = A7EAD5761CD5C679008781F6 /* StyleWatcher.swift */; }; A7EAD5781CD5C679008781F6 /* StyleWatcher.swift in Sources */ = {isa = PBXBuildFile; fileRef = A7EAD5761CD5C679008781F6 /* StyleWatcher.swift */; }; @@ -64,7 +61,6 @@ A784B22A1C74171B004EB35B /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; A784B2341C7417B3004EB35B /* DynamicTypeManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DynamicTypeManager.swift; sourceTree = ""; }; A784B2371C741BED004EB35B /* DynamicTypeElement.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DynamicTypeElement.swift; sourceTree = ""; }; - A784B23A1C741E84004EB35B /* NSMapTableExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NSMapTableExtensions.swift; sourceTree = ""; }; A784B2421C743306004EB35B /* ExampleApp.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = ExampleApp.app; sourceTree = BUILT_PRODUCTS_DIR; }; A784B2441C743306004EB35B /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; A784B2461C743306004EB35B /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; @@ -153,7 +149,6 @@ A784B2371C741BED004EB35B /* DynamicTypeElement.swift */, A784B2341C7417B3004EB35B /* DynamicTypeManager.swift */, A7EAD56B1CD5B0D0008781F6 /* Extensions.swift */, - A784B23A1C741E84004EB35B /* NSMapTableExtensions.swift */, A79DBE9A1CB99AF8000BAA63 /* StyleConfig.swift */, A7EAD5761CD5C679008781F6 /* StyleWatcher.swift */, A7EAD5681CD5AFFE008781F6 /* Util.swift */, @@ -249,14 +244,16 @@ isa = PBXProject; attributes = { LastSwiftUpdateCheck = 0720; - LastUpgradeCheck = 0720; + LastUpgradeCheck = 0800; ORGANIZATIONNAME = Tallwave; TargetAttributes = { A784B2181C74171B004EB35B = { CreatedOnToolsVersion = 7.2.1; + LastSwiftMigration = 0800; }; A784B2221C74171B004EB35B = { CreatedOnToolsVersion = 7.2.1; + LastSwiftMigration = 0800; }; A784B2411C743306004EB35B = { CreatedOnToolsVersion = 7.2.1; @@ -317,7 +314,6 @@ files = ( A79DBE9B1CB99AF8000BAA63 /* StyleConfig.swift in Sources */, A7EAD5691CD5AFFE008781F6 /* Util.swift in Sources */, - A784B23B1C741E84004EB35B /* NSMapTableExtensions.swift in Sources */, A779B0881C8F67B50044996A /* DynamicFontRegistry.swift in Sources */, A7EAD5771CD5C679008781F6 /* StyleWatcher.swift in Sources */, A784B2381C741BED004EB35B /* DynamicTypeElement.swift in Sources */, @@ -333,13 +329,12 @@ A7EAD5781CD5C679008781F6 /* StyleWatcher.swift in Sources */, A779B0891C8F67B50044996A /* DynamicFontRegistry.swift in Sources */, A7EAD56D1CD5B0D0008781F6 /* Extensions.swift in Sources */, - A784B23C1C741E84004EB35B /* NSMapTableExtensions.swift in Sources */, A784B2291C74171B004EB35B /* GliphyTests.swift in Sources */, A7EAD56A1CD5AFFF008781F6 /* Util.swift in Sources */, + 646FEAE51D92B89600375EB7 /* DynamicTypeElement.swift in Sources */, A779B08D1C8F69DA0044996A /* FontRegistryTests.swift in Sources */, A79DBE9C1CB99AF8000BAA63 /* StyleConfig.swift in Sources */, A784B2361C7417B3004EB35B /* DynamicTypeManager.swift in Sources */, - A784B2391C741BED004EB35B /* DynamicTypeElement.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -353,7 +348,6 @@ A784B2451C743306004EB35B /* AppDelegate.swift in Sources */, A7EAD5731CD5BD6E008781F6 /* DynamicTypeElement.swift in Sources */, A779B08A1C8F67B50044996A /* DynamicFontRegistry.swift in Sources */, - A7EAD5741CD5BD6E008781F6 /* NSMapTableExtensions.swift in Sources */, A7EAD5791CD5C679008781F6 /* StyleWatcher.swift in Sources */, A7EAD5751CD5BD6E008781F6 /* Util.swift in Sources */, A7EAD5711CD5BD6E008781F6 /* StyleConfig.swift in Sources */, @@ -404,8 +398,10 @@ CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; @@ -433,6 +429,7 @@ ONLY_ACTIVE_ARCH = YES; SDKROOT = iphoneos; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_VERSION = 3.0; TARGETED_DEVICE_FAMILY = "1,2"; VERSIONING_SYSTEM = "apple-generic"; VERSION_INFO_PREFIX = ""; @@ -452,8 +449,10 @@ CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; CLANG_WARN_EMPTY_BODY = YES; CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; CLANG_WARN_INT_CONVERSION = YES; CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_SUSPICIOUS_MOVE = YES; CLANG_WARN_UNREACHABLE_CODE = YES; CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; @@ -473,6 +472,8 @@ IPHONEOS_DEPLOYMENT_TARGET = 8.0; MTL_ENABLE_DEBUG_INFO = NO; SDKROOT = iphoneos; + SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; + SWIFT_VERSION = 3.0; TARGETED_DEVICE_FAMILY = "1,2"; VALIDATE_PRODUCT = YES; VERSIONING_SYSTEM = "apple-generic"; @@ -484,6 +485,7 @@ isa = XCBuildConfiguration; buildSettings = { CLANG_ENABLE_MODULES = YES; + "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = ""; DEFINES_MODULE = YES; DYLIB_COMPATIBILITY_VERSION = 1; DYLIB_CURRENT_VERSION = 1; @@ -503,6 +505,7 @@ isa = XCBuildConfiguration; buildSettings = { CLANG_ENABLE_MODULES = YES; + "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = ""; DEFINES_MODULE = YES; DYLIB_COMPATIBILITY_VERSION = 1; DYLIB_CURRENT_VERSION = 1; diff --git a/Gliphy.xcodeproj/xcshareddata/xcschemes/Gliphy.xcscheme b/Gliphy.xcodeproj/xcshareddata/xcschemes/Gliphy.xcscheme index ae801b9..3301ae4 100644 --- a/Gliphy.xcodeproj/xcshareddata/xcschemes/Gliphy.xcscheme +++ b/Gliphy.xcodeproj/xcshareddata/xcschemes/Gliphy.xcscheme @@ -1,6 +1,6 @@ CGFloat? { + func scaledFontSizeForStyle(_ textStyle: UIFontTextStyle) -> CGFloat? { if let descriptor = styleDictionary[textStyle] { - let baseFont = UIFont.preferredFontForTextStyle(descriptor.originalTextStyle) + let baseFont = UIFont.preferredFont(forTextStyle: descriptor.originalTextStyle) return baseFont.pointSize * CGFloat(descriptor.scaleAmount) } return nil diff --git a/Sources/Core/DynamicTypeElement.swift b/Sources/Core/DynamicTypeElement.swift index 5207d14..5496785 100644 --- a/Sources/Core/DynamicTypeElement.swift +++ b/Sources/Core/DynamicTypeElement.swift @@ -7,18 +7,19 @@ // import Foundation +import UIKit /** A wrapper around all of the attributes needed when the content size updates. Not a Struct because those can't be stored in `NSMapTable`. */ class DynamicTypeElement { let keyPath: String - let textStyle: String + let textStyle: UIFontTextStyle let fontName: String - init(keyPath: String, textStyle: String, fontName: String) { + init(keyPath: String, textStyle: UIFontTextStyle, fontName: String) { self.keyPath = keyPath self.textStyle = textStyle self.fontName = fontName } -} \ No newline at end of file +} diff --git a/Sources/Core/DynamicTypeManager.swift b/Sources/Core/DynamicTypeManager.swift index 12b3d0b..7cbf607 100644 --- a/Sources/Core/DynamicTypeManager.swift +++ b/Sources/Core/DynamicTypeManager.swift @@ -14,7 +14,7 @@ typealias Gliphy = DynamicTypeManager /** `DynamicTypeManager` is a singleton that watches for the `UIContentSizeCategoryDidChangeNotification` notification and then updates all views to the new size accordingly. By default, Dynamic Type only works with the system font. Gliphy allows any font installed to be substituted. */ -public class DynamicTypeManager { +open class DynamicTypeManager { struct Values { static let fontKeyPathUILabel = "font" @@ -26,20 +26,20 @@ public class DynamicTypeManager { /** The singleton instance of `DynamicTypeManager`. */ - public static let sharedInstance = DynamicTypeManager() + open static let sharedInstance = DynamicTypeManager() /// Storage and lookup for the views `DynamicTypeManager` is tracking. - private var elementToTypeTable: NSMapTable = NSMapTable.weakToStrongObjectsMapTable() + fileprivate var elementToTypeTable: NSMapTable = NSMapTable.weakToStrongObjects() - private init() { - NSNotificationCenter.defaultCenter().addObserver(self, + fileprivate init() { + NotificationCenter.default.addObserver(self, selector: #selector(DynamicTypeManager.contentSizeCategoryDidChange(_:)), - name: UIContentSizeCategoryDidChangeNotification, + name: NSNotification.Name.UIContentSizeCategoryDidChange, object: nil) } deinit { - NSNotificationCenter.defaultCenter().removeObserver(self) + NotificationCenter.default.removeObserver(self) } /** @@ -49,7 +49,7 @@ public class DynamicTypeManager { - Parameter textStyle: The equivalent text style to size against. - Parameter fontName: The name of the custom font to use. */ - public func watchLabel(label: UILabel, textStyle: String, fontName: String) { + open func watchLabel(_ label: UILabel, textStyle: UIFontTextStyle, fontName: String) { watchElement(label, fontKeyPath: Values.fontKeyPathUILabel, textStyle: textStyle, fontName: fontName) } @@ -60,7 +60,7 @@ public class DynamicTypeManager { - Parameter textStyle: The equivalent text style to size against. - Parameter fontName: The name of the custom font to use. */ - public func watchButton(button: UIButton, textStyle: String, fontName: String) { + open func watchButton(_ button: UIButton, textStyle: UIFontTextStyle, fontName: String) { watchElement(button, fontKeyPath: Values.fontKeyPathUIButton, textStyle: textStyle, fontName: fontName) } @@ -71,7 +71,7 @@ public class DynamicTypeManager { - Parameter textStyle: The equivalent text style to size against. - Parameter fontName: The name of the custom font to use. */ - public func watchTextField(textField: UITextField, textStyle: String, fontName: String) { + open func watchTextField(_ textField: UITextField, textStyle: UIFontTextStyle, fontName: String) { watchElement(textField, fontKeyPath: Values.fontKeyPathTextField, textStyle: textStyle, fontName: fontName) } @@ -82,7 +82,7 @@ public class DynamicTypeManager { - Parameter textStyle: The equivalent text style to size against. - Parameter fontName: The name of the custom font to use. */ - public func watchTextView(textView: UITextView, textStyle: String, fontName: String) { + open func watchTextView(_ textView: UITextView, textStyle: UIFontTextStyle, fontName: String) { watchElement(textView, fontKeyPath: Values.fontKeyPathTextView, textStyle: textStyle, fontName: fontName) } @@ -94,7 +94,7 @@ public class DynamicTypeManager { - Parameter textStyle: The equivalent text style to size against. - Parameter fontName: The name of the custom font to use. */ - public func watchElement(view: UIView, fontKeyPath: String, textStyle: String, fontName: String) { + open func watchElement(_ view: UIView, fontKeyPath: String, textStyle: UIFontTextStyle, fontName: String) { view.setValue(fontForTextStyle(textStyle, fontName: fontName), forKeyPath: fontKeyPath) let typeElement = DynamicTypeElement(keyPath: fontKeyPath, textStyle: textStyle, fontName: fontName) elementToTypeTable.setObject(typeElement, forKey: view) @@ -108,12 +108,12 @@ public class DynamicTypeManager { - Returns: The font with the `fontName` and size defined by the preferred font with `style`. If the `fontName` is not a valid font on the device, the system font for the `style` is used. */ - func fontForTextStyle(style: String, fontName: String) -> UIFont { + func fontForTextStyle(_ style: UIFontTextStyle, fontName: String) -> UIFont { if let customSize = DynamicFontRegistry.registry.scaledFontSizeForStyle(style) { return UIFont(name: fontName, size: customSize)! } - let systemFont = UIFont.preferredFontForTextStyle(style) + let systemFont = UIFont.preferredFont(forTextStyle: style) guard let customFont = UIFont(name: fontName, size: systemFont.pointSize) else { return systemFont } @@ -123,10 +123,10 @@ public class DynamicTypeManager { /** Catches the notification for `UIContentSizeCategoryDidChangeNotification`. Updates all of the views stored in the internal storage. */ - @objc public func contentSizeCategoryDidChange(notification: NSNotification) { + @objc open func contentSizeCategoryDidChange(_ notification: Notification) { let enumerator = elementToTypeTable.keyEnumerator() while let view = enumerator.nextObject() as? UIView { - if let element = elementToTypeTable[view] as? DynamicTypeElement { + if let element = elementToTypeTable.object(forKey: view) as? DynamicTypeElement { let font = fontForTextStyle(element.textStyle, fontName: element.fontName) view.setValue(font, forKeyPath: element.keyPath) } diff --git a/Sources/Core/Extensions.swift b/Sources/Core/Extensions.swift index 4e6a42c..b48d87a 100644 --- a/Sources/Core/Extensions.swift +++ b/Sources/Core/Extensions.swift @@ -11,26 +11,38 @@ import UIKit // These extensions are shortcuts to get the textStyle that was set for Dynamic Type extension UILabel { - var textStyle: String? { - return font.fontDescriptor().fontAttributes()[Util.dynamicTextAttribute] as? String + var textStyle: UIFontTextStyle? { + guard let fontTextStyleString = font?.fontDescriptor.fontAttributes[Util.dynamicTextAttribute] as? String else { + return nil + } + return UIFontTextStyle(rawValue: fontTextStyleString) } } extension UIButton { - var textStyle: String? { - let key = Util.dynamicTextAttribute - return titleLabel?.font.fontDescriptor().fontAttributes()[key] as? String + var textStyle: UIFontTextStyle? { + guard let fontTextStyleString = titleLabel?.font?.fontDescriptor.fontAttributes[Util.dynamicTextAttribute] as? String else { + return nil + } + return UIFontTextStyle(rawValue: fontTextStyleString) + } } extension UITextField { - var textStyle: String? { - return font?.fontDescriptor().fontAttributes()[Util.dynamicTextAttribute] as? String + var textStyle: UIFontTextStyle? { + guard let fontTextStyleString = font?.fontDescriptor.fontAttributes[Util.dynamicTextAttribute] as? String else { + return nil + } + return UIFontTextStyle(rawValue: fontTextStyleString) } } extension UITextView { - var textStyle: String? { - return font?.fontDescriptor().fontAttributes()[Util.dynamicTextAttribute] as? String + var textStyle: UIFontTextStyle? { + guard let fontTextStyleString = font?.fontDescriptor.fontAttributes[Util.dynamicTextAttribute] as? String else { + return nil + } + return UIFontTextStyle(rawValue: fontTextStyleString) } } diff --git a/Sources/Core/NSMapTableExtensions.swift b/Sources/Core/NSMapTableExtensions.swift deleted file mode 100644 index abf7fdd..0000000 --- a/Sources/Core/NSMapTableExtensions.swift +++ /dev/null @@ -1,27 +0,0 @@ -// -// NSMapTableExtensions.swift -// Gliphy -// -// Created by Scott Williams on 2/16/16. -// Copyright © 2016 Tallwave. All rights reserved. -// -// Taken from http://nshipster.com/nshashtable-and-nsmaptable/ - -import Foundation - -/// Allows subscripting on an `NSMapTable`. -extension NSMapTable { - subscript(key: AnyObject) -> AnyObject? { - get { - return objectForKey(key) - } - - set { - if newValue != nil { - setObject(newValue, forKey: key) - } else { - removeObjectForKey(key) - } - } - } -} \ No newline at end of file diff --git a/Sources/Core/StyleConfig.swift b/Sources/Core/StyleConfig.swift index 638383a..d9f5e95 100644 --- a/Sources/Core/StyleConfig.swift +++ b/Sources/Core/StyleConfig.swift @@ -10,7 +10,7 @@ import UIKit /// Keys of the dictionary are the text styles, values are the names of the custom font. /// Example: dictionary[UIFontTextStyleHeadline] = "Verdana" -public typealias StyleConfigDictionary = [String: String?] // textStyle: customFontName +public typealias StyleConfigDictionary = [UIFontTextStyle: String?] // textStyle: customFontName /// Container for all of the views that are supported by the DynamicTypeManager. public struct StyleConfig { diff --git a/Sources/Core/StyleWatcher.swift b/Sources/Core/StyleWatcher.swift index fb910dd..361c4a8 100644 --- a/Sources/Core/StyleWatcher.swift +++ b/Sources/Core/StyleWatcher.swift @@ -46,10 +46,10 @@ public struct StyleWatcher { - Parameter button: The button to watch. - Parameter withConfig: A `StyleConfig` to use if the button's style stored within. */ - public func watchButton(button: UIButton, withConfig config: StyleConfig) { + public func watchButton(_ button: UIButton, withConfig config: StyleConfig) { guard let textStyle = button.textStyle, - customFontName = config.button[textStyle], - fontName = customFontName else { return } + let customFontName = config.button[textStyle], + let fontName = customFontName else { return } Gliphy.sharedInstance.watchButton(button, textStyle: textStyle, fontName: fontName) } @@ -59,10 +59,10 @@ public struct StyleWatcher { - Parameter label: The button to watch. - Parameter withConfig: A `StyleConfig` to use if the button's style stored within. */ - public func watchLabel(label: UILabel, withConfig config: StyleConfig) { + public func watchLabel(_ label: UILabel, withConfig config: StyleConfig) { guard let textStyle = label.textStyle, - customFontName = config.label[textStyle], - fontName = customFontName else { return } + let customFontName = config.label[textStyle], + let fontName = customFontName else { return } Gliphy.sharedInstance.watchLabel(label, textStyle: textStyle, fontName: fontName) } @@ -72,10 +72,10 @@ public struct StyleWatcher { - Parameter textField: The text field to watch. - Parameter withConfig: A `StyleConfig` to use if the button's style stored within. */ - public func watchTextField(textField: UITextField, withConfig config: StyleConfig) { + public func watchTextField(_ textField: UITextField, withConfig config: StyleConfig) { guard let textStyle = textField.textStyle, - customFontName = config.textField[textStyle], - fontName = customFontName else { return } + let customFontName = config.textField[textStyle], + let fontName = customFontName else { return } Gliphy.sharedInstance.watchTextField(textField, textStyle: textStyle, fontName: fontName) } @@ -85,10 +85,10 @@ public struct StyleWatcher { - Parameter textView: The text view to watch. - Parameter withConfig: A `StyleConfig` to use if the button's style stored within. */ - public func watchTextView(textView: UITextView, withConfig config: StyleConfig) { + public func watchTextView(_ textView: UITextView, withConfig config: StyleConfig) { guard let textStyle = textView.textStyle, - customFontName = config.textView[textStyle], - fontName = customFontName else { return } + let customFontName = config.textView[textStyle], + let fontName = customFontName else { return } Gliphy.sharedInstance.watchTextView(textView, textStyle: textStyle, fontName: fontName) } }