-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfontPickerViewController.py
265 lines (221 loc) · 9.14 KB
/
fontPickerViewController.py
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
"""
note: Storyboard 実装なし
"""
import ctypes
from pyrubicon.objc.api import ObjCClass
from pyrubicon.objc.api import objc_method, objc_property
from pyrubicon.objc.runtime import send_super, SEL
from rbedge.enumerations import (
UIButtonType,
UIControlState,
UIControlEvents,
UILayoutConstraintAxis,
NSTextAlignment,
NSLineBreakMode,
UIUserInterfaceIdiom,
UIFontDescriptorSymbolicTraits,
UIModalPresentationStyle,
)
from rbedge.globalVariables import NSAttributedStringKey
from rbedge.functions import NSStringFromClass
from rbedge import pdbr
from pyLocalizedString import localizedString
UIViewController = ObjCClass('UIViewController')
NSLayoutConstraint = ObjCClass('NSLayoutConstraint')
UIColor = ObjCClass('UIColor')
UIFontPickerViewController = ObjCClass('UIFontPickerViewController')
UIFontPickerViewControllerConfiguration = ObjCClass(
'UIFontPickerViewControllerConfiguration')
UITextFormattingCoordinator = ObjCClass('UITextFormattingCoordinator')
UIButton = ObjCClass('UIButton')
UILabel = ObjCClass('UILabel')
NSDictionary = ObjCClass('NSDictionary')
UIFont = ObjCClass('UIFont')
class FontPickerViewController(UIViewController):
fontLabel: UILabel = objc_property()
textFormatter: UITextFormattingCoordinator = objc_property()
fontPicker: UIFontPickerViewController = objc_property()
@objc_method
def dealloc(self):
# xxx: 呼ばない-> `send_super(__class__, self, 'dealloc')`
print(f'\t- {NSStringFromClass(__class__)}: dealloc')
# MARK: - View Life Cycle
@objc_method
def viewDidLoad(self):
send_super(__class__, self, 'viewDidLoad')
self.navigationItem.title = localizedString('FontPickerTitle') if (
title := self.navigationItem.title) is None else title
self.view.backgroundColor = UIColor.systemBackgroundColor()
# --- fontPickerButton
fontPickerButton = UIButton.buttonWithType_(UIButtonType.system)
fontPickerButton.setTitle('UIFontPickerViewController',
forState=UIControlState.normal)
fontPickerButton.addTarget_action_forControlEvents_(
self, SEL('presentFontPicker:'), UIControlEvents.touchUpInside)
# --- textFormatterButton
textFormatterButton = UIButton.buttonWithType_(UIButtonType.system)
textFormatterButton.setTitle('UITextFormattingCoordinator',
forState=UIControlState.normal)
textFormatterButton.addTarget_action_forControlEvents_(
self, SEL('presentTextFormattingCoordinator:'),
UIControlEvents.touchUpInside)
# --- fontLabel
fontLabel = UILabel.new()
fontLabel.setContentHuggingPriority_forAxis_(
251.0, UILayoutConstraintAxis.horizontal)
fontLabel.setContentHuggingPriority_forAxis_(
251.0, UILayoutConstraintAxis.vertical)
fontLabel.textAlignment = NSTextAlignment.center
fontLabel.lineBreakMode = NSLineBreakMode.byTruncatingTail
fontLabel.font = UIFont.systemFontOfSize_(28.0)
fontLabel.text = localizedString('SampleFontTitle')
self.view.addSubview_(fontPickerButton)
fontPickerButton.translatesAutoresizingMaskIntoConstraints = False
self.view.addSubview_(textFormatterButton)
textFormatterButton.translatesAutoresizingMaskIntoConstraints = False
self.view.addSubview_(fontLabel)
fontLabel.translatesAutoresizingMaskIntoConstraints = False
# --- Layout
safeAreaLayoutGuide = self.view.safeAreaLayoutGuide
layoutMarginsGuide = self.view.layoutMarginsGuide
NSLayoutConstraint.activateConstraints_([
fontLabel.heightAnchor.constraintEqualToConstant_(62.0),
])
NSLayoutConstraint.activateConstraints_([
textFormatterButton.centerXAnchor.constraintEqualToAnchor_(
safeAreaLayoutGuide.centerXAnchor),
fontPickerButton.centerXAnchor.constraintEqualToAnchor_(
safeAreaLayoutGuide.centerXAnchor),
textFormatterButton.topAnchor.constraintEqualToAnchor_constant_(
fontPickerButton.bottomAnchor, 8.0),
fontLabel.leadingAnchor.constraintEqualToAnchor_constant_(
safeAreaLayoutGuide.leadingAnchor, 16.0),
fontPickerButton.topAnchor.constraintEqualToAnchor_constant_(
safeAreaLayoutGuide.topAnchor, 17.0),
fontLabel.trailingAnchor.constraintEqualToAnchor_constant_(
safeAreaLayoutGuide.trailingAnchor, -16.0),
fontLabel.topAnchor.constraintEqualToAnchor_constant_(
textFormatterButton.bottomAnchor, 20.0),
])
self.fontLabel = fontLabel
self.configureFontPicker()
if self.navigationController.traitCollection.userInterfaceIdiom != UIUserInterfaceIdiom.mac:
# UITextFormattingCoordinator's toggleFontPanel is available only for macOS.
textFormatterButton.setHidden_(True)
@objc_method
def viewWillAppear_(self, animated: bool):
send_super(__class__,
self,
'viewWillAppear:',
animated,
argtypes=[
ctypes.c_bool,
])
#print(f'\t{NSStringFromClass(__class__)}: viewWillAppear_')
@objc_method
def viewDidAppear_(self, animated: bool):
send_super(__class__,
self,
'viewDidAppear:',
animated,
argtypes=[
ctypes.c_bool,
])
#print(f'\t{NSStringFromClass(__class__)}: viewDidAppear_')
@objc_method
def viewWillDisappear_(self, animated: bool):
send_super(__class__,
self,
'viewWillDisappear:',
animated,
argtypes=[
ctypes.c_bool,
])
# print(f'\t{NSStringFromClass(__class__)}: viewWillDisappear_')
@objc_method
def viewDidDisappear_(self, animated: bool):
send_super(__class__,
self,
'viewDidDisappear:',
animated,
argtypes=[
ctypes.c_bool,
])
print(f'\t{NSStringFromClass(__class__)}: viewDidDisappear_')
@objc_method
def didReceiveMemoryWarning(self):
send_super(__class__, self, 'didReceiveMemoryWarning')
print(f'{__class__}: didReceiveMemoryWarning')
@objc_method
def configureFontPicker(self):
configuration = UIFontPickerViewControllerConfiguration.new()
configuration.includeFaces = True
configuration.displayUsingSystemFont = False
"""
xxx: 以下がピックアップされてる?
- UIFontDescriptorClassOldStyleSerifs
- UIFontDescriptorClassTransitionalSerifs
- UIFontDescriptorClassModernSerifs
- UIFontDescriptorClassSlabSerifs
- UIFontDescriptorClassFreeformSerifs
- UIFontDescriptorClassOrnamentals
- UIFontDescriptorClassScripts
"""
configuration.filteredTraits = UIFontDescriptorSymbolicTraits.classModernSerifs
fontPicker = UIFontPickerViewController.alloc().initWithConfiguration_(
configuration)
fontPicker.delegate = self
fontPicker.modalPresentationStyle = UIModalPresentationStyle.popover
self.fontPicker = fontPicker
@objc_method
def configureTextFormatter(self):
# xxx: `UITextFormattingCoordinator` 検証不可のため、未確認
if self.textFormatter is None:
if (scene := self.view.window().windowScene) is None:
return
attributes = NSDictionary.dictionaryWithObject(
self.fontLabel.font, forKey=NSAttributedStringKey.font)
textFormatter = UITextFormattingCoordinator.alloc().initWithWindowScene_(
scene)
textFormatter.delegate = self
textFormatter.setSelectedAttributes_isMultiple_(attributes, True)
self.textFormatter = textFormatter
@objc_method
def presentFontPicker_(self, sender):
if (button := sender).isKindOfClass_(UIButton):
popover = self.fontPicker.popoverPresentationController()
popover.sourceView = button
self.presentViewController(self.fontPicker,
animated=True,
completion=None)
@objc_method
def presentTextFormattingCoordinator_(self, sender):
# xxx: `UITextFormattingCoordinator` 検証不可のため、未確認
if not UITextFormattingCoordinator.isFontPanelVisible():
UITextFormattingCoordinator.toggleFontPanel_(sender)
# MARK: - UIFontPickerViewControllerDelegate
@objc_method
def fontPickerViewControllerDidCancel_(self, viewController):
pass
@objc_method
def fontPickerViewControllerDidPickFont_(self, viewController):
if (fontDescriptor := viewController.selectedFontDescriptor) is None:
return
else:
font = UIFont.fontWithDescriptor_size_(fontDescriptor, 28.0)
self.fontLabel.font = font
# MARK: - UITextFormattingCoordinatorDelegate
@objc_method
def updateTextAttributesWithConversionHandler_(self, conversionHandler):
# xxx: `UITextFormattingCoordinator` 検証不可のため、未確認
print(conversionHandler)
if __name__ == '__main__':
from rbedge.app import App
from rbedge.enumerations import UIModalPresentationStyle
main_vc = FontPickerViewController.new()
_title = NSStringFromClass(FontPickerViewController)
main_vc.navigationItem.title = _title
#presentation_style = UIModalPresentationStyle.fullScreen
presentation_style = UIModalPresentationStyle.pageSheet
app = App(main_vc, presentation_style)
app.present()