Skip to content

Commit

Permalink
Add new size method to get valye by exact device screen size
Browse files Browse the repository at this point in the history
  • Loading branch information
anatoliyv committed Jun 9, 2016
1 parent 492fe63 commit 17d00e0
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 13 deletions.
2 changes: 1 addition & 1 deletion AssistantKit.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
51 changes: 48 additions & 3 deletions AssistantKit/Classes/Screen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -174,17 +174,17 @@ extension Device {
extension Device {

/// Returns size for a specific device (iPad or iPhone/iPod)
static public func size<T: AnyObject>(phone: T, pad: T) -> T {
static public func size<T: AnyObject>(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<T: AnyObject>(small: T, medium: T, big: T) -> T {
static public func size<T: AnyObject>(small small: T, medium: T, big: T) -> T {
let family = Device.screen.family

switch family {
Expand All @@ -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<T: AnyObject>(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
}

}
2 changes: 2 additions & 0 deletions Example/AssistantKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand All @@ -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;
};
Expand Down
36 changes: 27 additions & 9 deletions Example/AssistantKit/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand All @@ -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")))
Expand Down Expand Up @@ -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))
}
}

0 comments on commit 17d00e0

Please sign in to comment.