From 17d00e0c2827c655726a29c23cf2c2800f963285 Mon Sep 17 00:00:00 2001 From: Anatoliy Voropay Date: Thu, 9 Jun 2016 08:54:26 +0300 Subject: [PATCH] Add new size method to get valye by exact device screen size --- AssistantKit.podspec | 2 +- AssistantKit/Classes/Screen.swift | 51 +++++++++++++++++-- .../AssistantKit.xcodeproj/project.pbxproj | 2 + Example/AssistantKit/ViewController.swift | 36 +++++++++---- 4 files changed, 78 insertions(+), 13 deletions(-) diff --git a/AssistantKit.podspec b/AssistantKit.podspec index 833b471..674812e 100644 --- a/AssistantKit.podspec +++ b/AssistantKit.podspec @@ -8,7 +8,7 @@ Pod::Spec.new do |s| s.name = 'AssistantKit' - s.version = '0.0.1' + s.version = '0.0.2' s.summary = 'Easy way to detect and work with  device environments written in Swift' # This description is used to generate tags and improve search results. diff --git a/AssistantKit/Classes/Screen.swift b/AssistantKit/Classes/Screen.swift index 385df81..d96c5b6 100644 --- a/AssistantKit/Classes/Screen.swift +++ b/AssistantKit/Classes/Screen.swift @@ -174,17 +174,17 @@ extension Device { extension Device { /// Returns size for a specific device (iPad or iPhone/iPod) - static public func size(phone: T, pad: T) -> T { + static public func size(phone phone: T, pad: T) -> T { return ( Device.isPad ? pad : phone ) } /** - Return size depending on specific screen size. + Return size depending on specific screen family. If Screen size is unknown (in this case ScreenFamily will be unknown too) it will return small value - Seealso: Screen, ScreenFamily */ - static public func size(small: T, medium: T, big: T) -> T { + static public func size(small small: T, medium: T, big: T) -> T { let family = Device.screen.family switch family { @@ -201,4 +201,49 @@ extension Device { return small } } + + /** + Return value for specific screen size. Incoming parameter should be a screen size. If it is not defined + nearest value will be used. Code example: + + ``` + let sizes: [Screen:AnyObject] = [ + .Inches_3_5: 12, + .Inches_4_0: 13, + .Inches_4_7: 14, + .Inches_9_7: 15 + ] + let exactSize = Device.size(sizes: sizes) as! Int + let _ = UIFont(name: "Arial", size: CGFloat(exactSize)) + ``` + + After that your font will be: + * 12 for 3.5" inches (older devices) + * 13 for iPhone 5, 5S + * 14 for iPhone 6, 6Plus and iPad mini + * and 15 for other iPads + + - Seealso: Screen + */ + static public func size(sizes sizes: [Screen : T]) -> T? { + let screen = Device.screen + var nearestValue: T? + var distance = CGFloat.max + + for (key, value) in sizes { + // Prevent from iterating whole array + if (key == screen) { + return value + } + + let actualDistance = fabs(key.rawValue - screen.rawValue) + if actualDistance < distance { + nearestValue = value + distance = actualDistance + } + } + + return nearestValue + } + } \ No newline at end of file diff --git a/Example/AssistantKit.xcodeproj/project.pbxproj b/Example/AssistantKit.xcodeproj/project.pbxproj index 2d82eb2..2054ed9 100644 --- a/Example/AssistantKit.xcodeproj/project.pbxproj +++ b/Example/AssistantKit.xcodeproj/project.pbxproj @@ -496,6 +496,7 @@ MODULE_NAME = ExampleApp; PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.demo.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; + TARGETED_DEVICE_FAMILY = "1,2"; }; name = Debug; }; @@ -509,6 +510,7 @@ MODULE_NAME = ExampleApp; PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.demo.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; + TARGETED_DEVICE_FAMILY = "1,2"; }; name = Release; }; diff --git a/Example/AssistantKit/ViewController.swift b/Example/AssistantKit/ViewController.swift index 499fef0..c7228bb 100644 --- a/Example/AssistantKit/ViewController.swift +++ b/Example/AssistantKit/ViewController.swift @@ -66,8 +66,17 @@ class ViewController: UIViewController { print() print("family: " + String(Device.screen.family.rawValue)) - print("size for device: " + String(Device.size("phone size", pad: "pad size"))) - print("size for family: " + String(Device.size("small family", medium: "medium family", big: "big family"))) + print("size for device: " + String(Device.size(phone: "phone size", pad: "pad size"))) + print("size for family: " + String(Device.size(small: "small family", medium: "medium family", big: "big family"))) + + let sizes: [Screen:AnyObject] = [ + .Inches_3_5: 12, + .Inches_4_0: 13, + .Inches_5_5: 14, + .Inches_9_7: 15 + ] + let exactSize = Device.size(sizes: sizes) as! Int + print("exact size: " + String(exactSize)) print() } @@ -86,9 +95,9 @@ class ViewController: UIViewController { let version = Device.osVersionString print("*** iOS") - print("OS: " + String(Device.osVersion)) - print("OS String: " + String(Device.osVersionString)) - print("> 9.0: " + String(Device.osVersionGreaterThan("9.0"))) + print("OS " + String(Device.osVersion)) + print("OS String " + String(Device.osVersionString)) + print("> 9.0 " + String(Device.osVersionGreaterThan("9.0"))) print("< 9.0 " + String(Device.osVersionLessThan("9.0"))) print("== " + String(Device.osVersionEqualTo(version))) print(">= 9.0 " + String(Device.osVersionEqualTo("9.0"))) @@ -154,11 +163,20 @@ class ViewController: UIViewController { default: print("Unknown scale") } - let size = Device.size(13, pad: 15) - let font = UIFont(name: "Arial", size: CGFloat(size)) + let size = Device.size(phone: 13, pad: 15) + let _ = UIFont(name: "Arial", size: CGFloat(size)) - let otherSize = Device.size(12, medium: 14, big: 15) - let otherFont = UIFont(name: "Arial", size: CGFloat(otherSize)) + let otherSize = Device.size(small: 12, medium: 14, big: 15) + let _ = UIFont(name: "Arial", size: CGFloat(otherSize)) + + let sizes: [Screen:AnyObject] = [ + .Inches_3_5: 12, + .Inches_4_0: 13, + .Inches_5_5: 14, + .Inches_9_7: 15 + ] + let exactSize = Device.size(sizes: sizes) as! Int + let _ = UIFont(name: "Arial", size: CGFloat(exactSize)) } }