Skip to content

Commit c55fd9b

Browse files
committed
完善Debug细节
1 parent f37e0e4 commit c55fd9b

4 files changed

Lines changed: 62 additions & 64 deletions

File tree

Demo/Demo/Debug/Debug.storyboard

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -283,15 +283,6 @@
283283
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="YqT-Ne-PTg">
284284
<rect key="frame" x="0.0" y="80" width="355" height="74"/>
285285
<subviews>
286-
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Font Size" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="Zjm-sq-ETl">
287-
<rect key="frame" x="0.0" y="0.0" width="65" height="30"/>
288-
<constraints>
289-
<constraint firstAttribute="height" constant="30" id="vJO-EN-4Q2"/>
290-
</constraints>
291-
<fontDescription key="fontDescription" type="system" weight="medium" pointSize="15"/>
292-
<nil key="textColor"/>
293-
<nil key="highlightedColor"/>
294-
</label>
295286
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="17.00" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="X44-wK-Ifh">
296287
<rect key="frame" x="85" y="6" width="35.333333333333343" height="18"/>
297288
<fontDescription key="fontDescription" type="system" pointSize="15"/>
@@ -307,6 +298,15 @@
307298
<action selector="fontSizeSliderAction:" destination="jSJ-t8-G4f" eventType="valueChanged" id="YLj-hi-R7e"/>
308299
</connections>
309300
</slider>
301+
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Font Size" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="Zjm-sq-ETl">
302+
<rect key="frame" x="0.0" y="0.0" width="65" height="30"/>
303+
<constraints>
304+
<constraint firstAttribute="height" constant="30" id="vJO-EN-4Q2"/>
305+
</constraints>
306+
<fontDescription key="fontDescription" type="system" weight="medium" pointSize="15"/>
307+
<nil key="textColor"/>
308+
<nil key="highlightedColor"/>
309+
</label>
310310
</subviews>
311311
<color key="backgroundColor" systemColor="systemBackgroundColor" cocoaTouchSystemColor="whiteColor"/>
312312
<constraints>
@@ -464,7 +464,7 @@
464464
<color key="textColor" systemColor="placeholderTextColor" red="0.23529411759999999" green="0.23529411759999999" blue="0.26274509800000001" alpha="0.29999999999999999" colorSpace="custom" customColorSpace="sRGB"/>
465465
<nil key="highlightedColor"/>
466466
</label>
467-
<slider opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" value="4" minValue="0.0" maxValue="4" translatesAutoresizingMaskIntoConstraints="NO" id="btd-hD-OsH">
467+
<slider opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" value="4" minValue="0.0" maxValue="5" translatesAutoresizingMaskIntoConstraints="NO" id="btd-hD-OsH">
468468
<rect key="frame" x="-2" y="30" width="359" height="45"/>
469469
<constraints>
470470
<constraint firstAttribute="height" constant="44" id="RZe-vt-cyA"/>
@@ -582,7 +582,7 @@
582582
<nil key="textColor"/>
583583
<nil key="highlightedColor"/>
584584
</label>
585-
<segmentedControl opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="top" segmentControlStyle="plain" selectedSegmentIndex="0" translatesAutoresizingMaskIntoConstraints="NO" id="wP6-Uj-eFq">
585+
<segmentedControl opaque="NO" contentMode="scaleToFill" enabled="NO" contentHorizontalAlignment="left" contentVerticalAlignment="top" segmentControlStyle="plain" selectedSegmentIndex="0" translatesAutoresizingMaskIntoConstraints="NO" id="wP6-Uj-eFq">
586586
<rect key="frame" x="0.0" y="36" width="355" height="32"/>
587587
<segments>
588588
<segment title="alignBaselines"/>
@@ -630,7 +630,7 @@
630630
<color key="textColor" systemColor="placeholderTextColor" red="0.23529411759999999" green="0.23529411759999999" blue="0.26274509800000001" alpha="0.29999999999999999" colorSpace="custom" customColorSpace="sRGB"/>
631631
<nil key="highlightedColor"/>
632632
</label>
633-
<slider opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" minValue="0.0" maxValue="1" translatesAutoresizingMaskIntoConstraints="NO" id="Fed-7V-E3t">
633+
<slider opaque="NO" contentMode="scaleToFill" enabled="NO" contentHorizontalAlignment="center" contentVerticalAlignment="center" minValue="0.0" maxValue="1" translatesAutoresizingMaskIntoConstraints="NO" id="Fed-7V-E3t">
634634
<rect key="frame" x="-2" y="30" width="359" height="45"/>
635635
<constraints>
636636
<constraint firstAttribute="height" constant="44" id="zYP-gX-HtO"/>
@@ -665,7 +665,7 @@
665665
<nil key="textColor"/>
666666
<nil key="highlightedColor"/>
667667
</label>
668-
<switch opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="750" verticalHuggingPriority="750" contentHorizontalAlignment="center" contentVerticalAlignment="center" translatesAutoresizingMaskIntoConstraints="NO" id="dpt-5T-qVV">
668+
<switch opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="750" verticalHuggingPriority="750" enabled="NO" contentHorizontalAlignment="center" contentVerticalAlignment="center" translatesAutoresizingMaskIntoConstraints="NO" id="dpt-5T-qVV">
669669
<rect key="frame" x="296" y="3.6666666666666288" width="51" height="31"/>
670670
<connections>
671671
<action selector="allowsDefaultTighteningForTruncationSwitchAction:" destination="jSJ-t8-G4f" eventType="valueChanged" id="q8T-zL-faS"/>
@@ -979,11 +979,18 @@
979979
</connections>
980980
</view>
981981
<navigationItem key="navigationItem" id="jAr-o0-mZe">
982-
<barButtonItem key="rightBarButtonItem" title="Save" id="OBc-dc-wiO">
983-
<connections>
984-
<action selector="saveAction:" destination="jSJ-t8-G4f" id="gSX-bH-Gi2"/>
985-
</connections>
986-
</barButtonItem>
982+
<rightBarButtonItems>
983+
<barButtonItem title="Save" id="OBc-dc-wiO">
984+
<connections>
985+
<action selector="saveAction:" destination="jSJ-t8-G4f" id="gSX-bH-Gi2"/>
986+
</connections>
987+
</barButtonItem>
988+
<barButtonItem title="Clean" id="g7j-cE-9wO">
989+
<connections>
990+
<action selector="cleanAction:" destination="jSJ-t8-G4f" id="t9d-zA-hPt"/>
991+
</connections>
992+
</barButtonItem>
993+
</rightBarButtonItems>
987994
</navigationItem>
988995
</viewController>
989996
<placeholder placeholderIdentifier="IBFirstResponder" id="skO-4L-z5s" userLabel="First Responder" customClass="UIResponder" sceneMemberID="firstResponder"/>

Demo/Demo/Debug/Debug.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,21 @@ extension Debug {
106106
}
107107
}
108108

109+
extension Debug.Label {
110+
111+
static let fonts: [UIFont] = [
112+
.systemFont(ofSize: 17.0),
113+
.systemFont(ofSize: 17.0, weight: .light),
114+
.systemFont(ofSize: 17.0, weight: .medium),
115+
.systemFont(ofSize: 17.0, weight: .semibold),
116+
.systemFont(ofSize: 17.0, weight: .black),
117+
UIFont(name: "Georgia", size: 17.0) ?? .systemFont(ofSize: 17.0),
118+
UIFont(name: "Helvetica", size: 17.0) ?? .systemFont(ofSize: 17.0),
119+
UIFont(name: "Helvetica Neue", size: 17.0) ?? .systemFont(ofSize: 17.0),
120+
UIFont(name: "Times New Roman", size: 17.0) ?? .systemFont(ofSize: 17.0)
121+
]
122+
}
123+
109124
private extension Data {
110125

111126
func map() -> UIFontDescriptor? {

Demo/Demo/Debug/DebugLabelView.swift

Lines changed: 17 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ class DebugLabelView: UIView {
2323
let value = font.withSize(fontSize)
2424
label.font = value
2525
fontNameLabel.text = value.fontName
26+
let index = Debug.Label.fonts.firstIndex(where: { $0.fontName == font.fontName })
27+
fontNameSlider.value = .init(index ?? 0)
2628
}
2729
}
2830
var fontSize: CGFloat = 17 {
@@ -67,7 +69,7 @@ class DebugLabelView: UIView {
6769
allowsDefaultTighteningForTruncationSwitch.isEnabled = value
6870
}
6971
}
70-
var baselineAdjustment: UIBaselineAdjustment = .none {
72+
var baselineAdjustment: UIBaselineAdjustment = .alignBaselines {
7173
didSet {
7274
let value = baselineAdjustment
7375
label.baselineAdjustment = value
@@ -149,7 +151,9 @@ class DebugLabelView: UIView {
149151

150152
override func awakeFromNib() {
151153
super.awakeFromNib()
152-
154+
// 设置字体名称滑块最大值
155+
fontNameSlider.maximumValue = .init(Debug.Label.fonts.count - 1)
156+
// 设置label监听
153157
labelBoundsObservation = label.observe(\.bounds) { [weak self] (object, changed) in
154158
guard let self = self else { return }
155159
// 设置当前宽高
@@ -203,39 +207,19 @@ extension DebugLabelView {
203207
}
204208

205209
// normal
206-
if let value = info.font {
207-
font = value
208-
fontSize = value.pointSize
209-
}
210-
if let value = info.numberOfLines {
211-
numberOfLines = value
212-
}
213-
if let value = info.textAlignment {
214-
textAlignment = value
215-
}
216-
if let value = info.lineBreakMode {
217-
lineBreakMode = value
218-
}
219-
if let value = info.adjustsFontSizeToFitWidth {
220-
adjustsFontSizeToFitWidth = value
221-
}
222-
if let value = info.baselineAdjustment {
223-
baselineAdjustment = value
224-
}
225-
if let value = info.minimumScaleFactor {
226-
minimumScaleFactor = value
227-
}
228-
if let value = info.allowsDefaultTighteningForTruncation {
229-
allowsDefaultTighteningForTruncation = value
230-
}
210+
font = info.font ?? .systemFont(ofSize: 17)
211+
fontSize = info.font?.pointSize ?? 17
212+
numberOfLines = info.numberOfLines ?? 0
213+
textAlignment = info.textAlignment ?? .natural
214+
lineBreakMode = info.lineBreakMode ?? .byTruncatingTail
215+
adjustsFontSizeToFitWidth = info.adjustsFontSizeToFitWidth ?? false
216+
baselineAdjustment = info.baselineAdjustment ?? .alignBaselines
217+
minimumScaleFactor = info.minimumScaleFactor ?? 0
218+
allowsDefaultTighteningForTruncation = info.allowsDefaultTighteningForTruncation ?? false
231219

232220
// paragraphs
233-
if let value = info.lineSpacing {
234-
lineSpacing = value
235-
}
236-
if let value = info.lineHeightMultiple {
237-
lineHeightMultiple = value
238-
}
221+
lineSpacing = info.lineSpacing ?? 0
222+
lineHeightMultiple = info.lineHeightMultiple ?? 0
239223

240224
// 刷新布局
241225
layoutIfNeeded()

Demo/Demo/Debug/DebugLabelViewController.swift

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,6 @@ private let key = "com.debug.label"
1313

1414
class DebugLabelViewController: ViewController<DebugLabelView> {
1515

16-
private let fonts: [UIFont] = [
17-
.systemFont(ofSize: 17.0),
18-
.systemFont(ofSize: 17.0, weight: .light),
19-
.systemFont(ofSize: 17.0, weight: .medium),
20-
.systemFont(ofSize: 17.0, weight: .semibold),
21-
.systemFont(ofSize: 17.0, weight: .black),
22-
UIFont(name: "Georgia", size: 17.0) ?? .systemFont(ofSize: 17.0),
23-
UIFont(name: "Helvetica", size: 17.0) ?? .systemFont(ofSize: 17.0),
24-
UIFont(name: "Helvetica Neue", size: 17.0) ?? .systemFont(ofSize: 17.0),
25-
UIFont(name: "Times New Roman", size: 17.0) ?? .systemFont(ofSize: 17.0)
26-
]
27-
2816
private var info: Debug.Label = .init() {
2917
didSet {
3018
container.set(info: info)
@@ -64,6 +52,10 @@ class DebugLabelViewController: ViewController<DebugLabelView> {
6452
guard let json = try? JSONEncoder().encode(info) else { return }
6553
UserDefaults.standard.setValue(json, forKey: key)
6654
}
55+
@IBAction func cleanAction(_ sender: UIBarButtonItem) {
56+
UserDefaults.standard.removeObject(forKey: key)
57+
info = .init()
58+
}
6759

6860
@IBAction func pageControlAction(_ sender: UIPageControl) {
6961
container.set(page: sender.currentPage, scroll: true)
@@ -83,7 +75,7 @@ class DebugLabelViewController: ViewController<DebugLabelView> {
8375
}
8476

8577
@IBAction func fontNameSliderAction(_ sender: UISlider) {
86-
info.font = fonts[.init(sender.value)].withSize(info.font?.pointSize ?? 17.0)
78+
info.font = Debug.Label.fonts[.init(sender.value)].withSize(info.font?.pointSize ?? 17.0)
8779
}
8880
@IBAction func fontSizeSliderAction(_ sender: UISlider) {
8981
info.font = info.font?.withSize(.init(sender.value))

0 commit comments

Comments
 (0)