-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContentSizeView.swift
More file actions
160 lines (120 loc) · 5.42 KB
/
ContentSizeView.swift
File metadata and controls
160 lines (120 loc) · 5.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
//
// ContentSizeCategoryView.swift
// FontBrowser
//
// Created by Daniel Krofchick on 2015-09-04.
// Copyright (c) 2015 Figure1. All rights reserved.
//
import UIKit
private let ei = UIEdgeInsets(top: 20.0, left: 20.0, bottom: 20.0, right: 20.0)
private let yi = CGFloat(5.0)
class ContentSizeView: UIView {
var label = UILabel()
var slider = ContentSizeSlider()
var lastValue = CGFloat(0)
var pointChangedBlock: (() -> ())?
var points = [CGFloat]()
var dynamicTypeObserver: DynamicTypeObserver?
override init(frame: CGRect) {
super.init(frame: frame)
backgroundColor = UIColor(white: 0.9, alpha: 1.0)
points = sliderPoints(UIApplication.contentSizeCategories().count)
slider.points = colorPoints()
label.textAlignment = .Center
addSubview(label)
slider.addTarget(self, action: Selector("sliderDidChange:"), forControlEvents: .ValueChanged)
addSubview(slider)
initConstraints()
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("contentSizeClassDidChange"), name: UIContentSizeCategoryDidChangeNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("contentSizeClassDidChange"), name: dynamicTypeObserverDidChangeNotification, object: nil)
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
func initConstraints() {
label.setTranslatesAutoresizingMaskIntoConstraints(false)
slider.setTranslatesAutoresizingMaskIntoConstraints(false)
addConstraint(NSLayoutConstraint(item: label, attribute: .Top, relatedBy: .Equal, toItem: self, attribute: .Top, multiplier: 1, constant: ei.top))
addConstraint(NSLayoutConstraint(item: label, attribute: .Left, relatedBy: .Equal, toItem: self, attribute: .Left, multiplier: 1, constant: ei.left))
addConstraint(NSLayoutConstraint(item: label, attribute: .Right, relatedBy: .Equal, toItem: self, attribute: .Right, multiplier: 1, constant: -ei.right))
addConstraint(NSLayoutConstraint(item: slider, attribute: .Top, relatedBy: .Equal, toItem: label, attribute: .Bottom, multiplier: 1, constant: yi))
addConstraint(NSLayoutConstraint(item: slider, attribute: .Left, relatedBy: .Equal, toItem: self, attribute: .Left, multiplier: 1, constant: ei.left))
addConstraint(NSLayoutConstraint(item: slider, attribute: .Right, relatedBy: .Equal, toItem: self, attribute: .Right, multiplier: 1, constant: -ei.right))
addConstraint(NSLayoutConstraint(item: slider, attribute: .Bottom, relatedBy: .Equal, toItem: self, attribute: .Bottom, multiplier: 1, constant: -ei.bottom))
}
@objc func contentSizeClassDidChange() {
slider.points = colorPoints()
}
func setCategory(category: String) {
setPoint(nearestPoint(pointForContentSizeCategory(category), points: points), animated: true)
}
func getCategory() -> String {
return contentSizeCategoryForPoint(nearestPoint(CGFloat(slider.value), points: points))
}
func sliderDidChange(slider: UISlider) {
setPoint(nearestPoint(CGFloat(slider.value), points: points), animated: true)
}
func setPoint(point: CGFloat, animated: Bool) {
slider.setValue(Float(point), animated: false)
if let index = find(points, point) {
label.text = UIApplication.nameForContentSizeCateogy(UIApplication.contentSizeCategories()[index])
}
if point != lastValue {
pointChangedBlock?()
lastValue = point
}
}
func colorPoints() -> [ColorPoint] {
var result = [ColorPoint]()
for (index, point) in enumerate(points) {
let category = UIApplication.contentSizeCategories()[index]
if let _ = dynamicTypeObserver?.preferredFonts[category] {
result.append(ColorPoint(value: point, color: UIColor.greenColor()))
} else {
result.append(ColorPoint(value: point, color: UIColor.redColor()))
}
}
return result
}
func pointForContentSizeCategory(category: String) -> CGFloat {
if let index = find(UIApplication.contentSizeCategories(), category) {
return points[index]
} else {
return CGFloat(0)
}
}
func contentSizeCategoryForPoint(point: CGFloat) -> String {
if let index = find(points, point) {
return UIApplication.contentSizeCategories()[index]
}
return ""
}
func nearestPoint(point: CGFloat, points: [CGFloat]) -> CGFloat {
var nearest = CGFloat(0)
var distance = CGFloat(1)
for p in points {
let d = point - p
if abs(d) < abs(distance) {
distance = d
nearest = p
}
if d < 0 {
return nearest
}
}
return nearest
}
func sliderPoints(n: Int) -> [CGFloat] {
var points = [CGFloat]()
let span = CGFloat(1) / CGFloat(n - 1)
var x = CGFloat(0)
while true {
points.append(x)
if x >= 1 {
break
}
x += span
}
return points
}
}