-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTASizeLabel.swift
198 lines (172 loc) · 5.84 KB
/
TASizeLabel.swift
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
//
// TASizeLabel.swift
// TestDemo
//
// Created by Thiha Aung on 10/17/18.
// Copyright © 2018 Thiha Aung. All rights reserved.
//
import UIKit
class TASizeLabel: UILabel {
public var screenWidth: CGFloat {
return UIScreen.main.bounds.size.width
}
public var screenHeight: CGFloat {
return UIScreen.main.bounds.size.height
}
public var screenOrientation: UIInterfaceOrientation {
return UIApplication.shared.statusBarOrientation
}
// The size constant which is defined at stylesheet, you may set up as you want
internal enum SizeLevel: Int {
case HeadLineOne = 0 // 48 points
case HeadLineTwo = 1// 36 points
case Biggest = 2 // 32 points
case Bigger = 3 // 24 points
case Big = 4 // 20 Points
case Medium = 5 // 18 points
case Normal = 6 // 16 points
case Small = 7 // 14 points
case Smaller = 8 // 12 points
}
// Normal device sizes
internal enum DeviceSize {
case Small
case Retina
case HDRetina
case SuperRetina
case Other
}
// Programmatically: use the enum
private var sizeLevel: SizeLevel = .Normal
@IBInspectable var fontSizeLevel:Int {
get {
return self.sizeLevel.rawValue
}
set(sizeIndex) {
self.sizeLevel = SizeLevel(rawValue: sizeIndex) ?? .Normal
}
}
private var fontSize : CGFloat = 0.0
private var pointSpecified: CGFloat = 0.0
private var isResized: Bool = false
// IB: use the adapter
@IBInspectable var requiredResize: Bool = false {
didSet{
self.isResized = requiredResize
}
}
@IBInspectable var kerningSpace: CGFloat = 0.0
// The minimum size that you want to shrink on smaller device [Small].
// Higher device will be automatically reduce to each based on that you defined.
@IBInspectable var minimumSize: Int = 0
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
}
override init(frame: CGRect) {
super.init(frame: frame)
}
override func awakeFromNib() {
commonInit()
}
func commonInit(){
self.controlFontSize(sizeLevel: sizeLevel, deviceSize: checkDevice())
self.addCharactersSpacing(kerningSpace)
}
// For checking iPhone screens resolution
private func checkDevice() -> DeviceSize{
// Device Check
if UIDevice().userInterfaceIdiom == .phone {
switch UIScreen.main.nativeBounds.height {
case 960:
// "iPhone 4/4S"
return .Small
case 1136:
// "iPhone 5/5S/5C"
return .Small
case 1334:
// "iPhone 6/6S/7/8"
return .Retina
case 1920, 2208:
// "iPhone 6+/6S+/7+/8+"
return .HDRetina
case 2436:
// "iPhone X, Xs"
return .SuperRetina
case 2688:
// "iPhone Xs Max"
return .SuperRetina
case 1792:
// "iPhone Xr"
return .SuperRetina
default:
return .Other
}
}else{
return .Other
}
}
func addCharactersSpacing(_ value: CGFloat) {
if let textString = text {
let attrs: [NSAttributedString.Key : Any] = [.kern: value]
attributedText = NSAttributedString(string: textString, attributes: attrs)
}
}
// Here lies the main changes
private func controlFontSize(sizeLevel: SizeLevel, deviceSize: DeviceSize){
// StyleSheet Font Sizes from higher to lower
// HeadLineOne = 48 points
// HeadLineTwo = 36 points
// Biggest = 32 points
// Bigger = 24 points
// Big = 20 Points
// Medium = 18 points
// Normal = 16 points
// Small = 14 points
// Smaller = 12 points
switch sizeLevel {
case .Smaller:
pointSpecified = 12
case .Small:
pointSpecified = 14
case .Normal:
pointSpecified = 16
case .Medium:
pointSpecified = 18
case .Big:
pointSpecified = 20
case .Bigger:
pointSpecified = 24
case .Biggest:
pointSpecified = 32
case .HeadLineTwo:
pointSpecified = 36
case .HeadLineOne:
pointSpecified = 48
}
self.fontSize = correctFontSizeForDevice(deviceSize: deviceSize, pointSpecified: pointSpecified)
self.font = UIFont(name: self.font.fontName, size: fontSize)
}
private func correctFontSizeForDevice (deviceSize : DeviceSize, pointSpecified: CGFloat) -> CGFloat{
switch deviceSize {
case .Small:
return checkReductionSize(minimumSize, pointSpecified, 15) // <= 15 is default set up, you can change as well
case .Retina:
return checkReductionSize(minimumSize - 5, pointSpecified, 10) // <= 10 is default set up, you can change as well
case .HDRetina:
return checkReductionSize(minimumSize - 10, pointSpecified, 5) // <= 5 is default set up, you can change as well
case .SuperRetina, .Other:
return pointSpecified
}
}
private func checkReductionSize(_ reductionSize: Int,_ pointSpecified: CGFloat,_ defaultReductionSize: Int) -> CGFloat{
if isResized{
if reductionSize != 0 && reductionSize > 0{
return pointSpecified - CGFloat(reductionSize)
}else{
return pointSpecified - CGFloat(defaultReductionSize)
}
}else{
return pointSpecified
}
}
}