-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathISHMapboxDynamicFontObserver.m
More file actions
188 lines (141 loc) · 6.48 KB
/
Copy pathISHMapboxDynamicFontObserver.m
File metadata and controls
188 lines (141 loc) · 6.48 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
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
//
// ISHMapboxDynamicFontObserver.m
// MapboxDynamicFontStyle
//
// Created by Felix Lamouroux on 18.08.17.
// Copyright © 2017 iosphere GmbH. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "ISHMapboxDynamicFontObserver.h"
@interface ISHMapboxDynamicFontObserver ()
@property (nonatomic) NSDictionary<NSString *, MGLStyleValue *> *baseTextSizeValues;
@property (nonatomic) NSDictionary<NSString *, MGLStyleValue *> *baseIconSizeValues;
@end
@implementation ISHMapboxDynamicFontObserver
#pragma mark - Setup
- (instancetype)init {
self = [super init];
if (self) {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(UIContentSizeCategoryDidChangeNotification:)
name:UIContentSizeCategoryDidChangeNotification
object:nil];
}
return self;
}
- (instancetype)initWithStyle:(MGLStyle *)style {
NSParameterAssert(style);
self = [self init];
self.style = style;
return self;
}
- (void)setStyle:(MGLStyle *)style {
_style = style;
if (!style) {
self.baseTextSizeValues = nil;
self.baseIconSizeValues = nil;
return;
}
[self setupBaseValuesForStyle:style];
[self updateStyleForPreferredContentSizeCategory];
}
- (void)setupBaseValuesForStyle:(MGLStyle *)style {
NSMutableDictionary<NSString *, MGLStyleValue *> *baseTextSizeValues = [NSMutableDictionary new];
NSMutableDictionary<NSString *, MGLStyleValue *> *baseIconSizeValues = [NSMutableDictionary new];
for (MGLSymbolStyleLayer *symbolLayer in style.layers) {
if (![symbolLayer isKindOfClass:[MGLSymbolStyleLayer class]]) {
continue;
}
if (!symbolLayer.textFontSize || !symbolLayer.identifier) {
continue;
}
baseTextSizeValues[symbolLayer.identifier] = symbolLayer.textFontSize;
if (!symbolLayer.iconImageName) {
continue;
}
baseIconSizeValues[symbolLayer.identifier] = symbolLayer.iconScale ? : [MGLStyleValue valueWithRawValue:@1];
}
self.baseTextSizeValues = [baseTextSizeValues copy];
self.baseIconSizeValues = [baseIconSizeValues copy];
}
#pragma mark - Dynamic Type Integration
- (void)UIContentSizeCategoryDidChangeNotification:(NSNotification *)note {
[self updateStyleForPreferredContentSizeCategory];
}
- (void)updateStyleForPreferredContentSizeCategory {
[self updateStyleForContentSizeCategory:[UIApplication sharedApplication].preferredContentSizeCategory];
}
- (void)updateStyleForContentSizeCategory:(UIContentSizeCategory)category {
CGFloat scale = [ISHMapboxDynamicFontObserver fontScaleForContentSizeCategory:category];
for (NSString *identifier in self.baseTextSizeValues) {
MGLStyleLayer *layer = [self.style layerWithIdentifier:identifier];
if (![layer isKindOfClass:[MGLSymbolStyleLayer class]]) {
continue;
}
MGLSymbolStyleLayer *symbolLayer = (MGLSymbolStyleLayer *)layer;
MGLStyleValue *textValue = [self.baseTextSizeValues objectForKey:identifier];
symbolLayer.textFontSize = [ISHMapboxDynamicFontObserver styleValue:textValue multipliedByScale:scale];
if (!symbolLayer.iconImageName) {
continue;
}
MGLStyleValue *iconScaleValue = [self.baseIconSizeValues objectForKey:identifier];
symbolLayer.iconScale = [ISHMapboxDynamicFontObserver styleValue:iconScaleValue multipliedByScale:scale];
}
}
#pragma mark - Style Value Scaling
+ (MGLStyleValue *)styleValue:(MGLStyleValue *)value multipliedByScale:(CGFloat)scale {
if ([value isKindOfClass:[MGLConstantStyleValue class]]) {
return [self styleConstantValue:(MGLConstantStyleValue *)value multipliedByScale:scale];
}
if ([value isKindOfClass:[MGLCameraStyleFunction class]]) {
return [self styleCameraValue:(MGLCameraStyleFunction *)value multipliedByScale:scale];
}
return value;
}
+ (MGLStyleValue *)styleConstantValue:(MGLConstantStyleValue *)constantValue multipliedByScale:(CGFloat)scale {
if (![constantValue.rawValue isKindOfClass:[NSNumber class]]) {
return constantValue;
}
return [MGLStyleValue valueWithRawValue:@([(NSNumber *)constantValue.rawValue doubleValue] * scale)];
}
+ (MGLStyleValue *)styleCameraValue:(MGLCameraStyleFunction *)cameraValue multipliedByScale:(CGFloat)scale {
__block NSMutableDictionary<id, MGLStyleValue *> *newStops = [NSMutableDictionary dictionaryWithCapacity:cameraValue.stops.count];
[cameraValue.stops enumerateKeysAndObjectsUsingBlock:^(id key, MGLStyleValue *value, BOOL *stop) {
newStops[key] = [self styleValue:value multipliedByScale:scale];
}];
MGLCameraStyleFunction *newValue = [MGLCameraStyleFunction valueWithInterpolationMode:cameraValue.interpolationMode
cameraStops:newStops
options:nil];
newValue.interpolationBase = cameraValue.interpolationBase;
return newValue ? : cameraValue;
}
+ (CGFloat)fontScaleForContentSizeCategory:(UIContentSizeCategory)category {
static NSDictionary<UIContentSizeCategory, NSNumber *> *scales;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
scales = @{
UIContentSizeCategoryExtraSmall: @0.7,
UIContentSizeCategorySmall: @0.8,
UIContentSizeCategoryMedium: @0.9,
UIContentSizeCategoryLarge: @1,
UIContentSizeCategoryExtraLarge: @1.15,
UIContentSizeCategoryExtraExtraLarge: @1.3,
UIContentSizeCategoryExtraExtraExtraLarge: @1.4,
UIContentSizeCategoryAccessibilityMedium: @1.7,
UIContentSizeCategoryAccessibilityExtraLarge: @2,
UIContentSizeCategoryAccessibilityExtraExtraLarge: @2.5,
UIContentSizeCategoryAccessibilityExtraExtraExtraLarge: @3,
};
if (@available(iOS 10.0, *)) {
NSMutableDictionary<UIContentSizeCategory, NSNumber *> *mutableScales = [scales mutableCopy];
[mutableScales setObject:@1 forKey:UIContentSizeCategoryUnspecified];
scales = [mutableScales copy];
}
});
NSNumber *scale = scales[category];
if (!scale) {
return 1;
}
return [scale doubleValue];
}
@end