Skip to content

Commit b47947c

Browse files
committed
尝试使用Neat的思路解决TextKit与UILabel行高不一致问题 附件问题待解决.
1 parent 96fb634 commit b47947c

5 files changed

Lines changed: 668 additions & 1 deletion

File tree

AttributedString.xcodeproj/project.pbxproj

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@
8585
9BE7D4C72488E0FC00DE1176 /* Action.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9BE7D4C52488E0FC00DE1176 /* Action.swift */; };
8686
9BE7D4C82488E0FC00DE1176 /* Action.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9BE7D4C52488E0FC00DE1176 /* Action.swift */; };
8787
9BE7D4C92488E0FC00DE1176 /* Action.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9BE7D4C52488E0FC00DE1176 /* Action.swift */; };
88+
9BEBB11324D511A000355C22 /* UILabelLayoutManagerDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9BEBB11224D511A000355C22 /* UILabelLayoutManagerDelegate.swift */; };
89+
9BEBB11424D511A000355C22 /* UILabelLayoutManagerDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9BEBB11224D511A000355C22 /* UILabelLayoutManagerDelegate.swift */; };
90+
9BEBB11624D5135D00355C22 /* UIFontExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9BEBB11524D5135D00355C22 /* UIFontExtension.swift */; };
91+
9BEBB11724D5135D00355C22 /* UIFontExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9BEBB11524D5135D00355C22 /* UIFontExtension.swift */; };
8892
/* End PBXBuildFile section */
8993

9094
/* Begin PBXContainerItemProxy section */
@@ -147,6 +151,8 @@
147151
9BA451E724B0412E00BE3F02 /* ArrayExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ArrayExtension.swift; sourceTree = "<group>"; };
148152
9BBC525E24A053A600CBDFFF /* Checking.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Checking.swift; sourceTree = "<group>"; };
149153
9BE7D4C52488E0FC00DE1176 /* Action.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Action.swift; sourceTree = "<group>"; };
154+
9BEBB11224D511A000355C22 /* UILabelLayoutManagerDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UILabelLayoutManagerDelegate.swift; sourceTree = "<group>"; };
155+
9BEBB11524D5135D00355C22 /* UIFontExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIFontExtension.swift; sourceTree = "<group>"; };
150156
/* End PBXFileReference section */
151157

152158
/* Begin PBXFrameworksBuildPhase section */
@@ -284,9 +290,9 @@
284290
9B6E89A42382739C009EBEBE /* UIKit */ = {
285291
isa = PBXGroup;
286292
children = (
293+
9BEBB11124D5113D00355C22 /* UILabel */,
287294
9B6E89A7238273E4009EBEBE /* UITextViewExtension.swift */,
288295
9B6E89A92382742F009EBEBE /* UITextFieldExtension.swift */,
289-
9B6E89AB238274CB009EBEBE /* UILabelExtension.swift */,
290296
9B0A2A39238524E1003D8A55 /* UIButtonExtension.swift */,
291297
);
292298
path = UIKit;
@@ -310,6 +316,16 @@
310316
path = CoreGraphics;
311317
sourceTree = "<group>";
312318
};
319+
9BEBB11124D5113D00355C22 /* UILabel */ = {
320+
isa = PBXGroup;
321+
children = (
322+
9B6E89AB238274CB009EBEBE /* UILabelExtension.swift */,
323+
9BEBB11224D511A000355C22 /* UILabelLayoutManagerDelegate.swift */,
324+
9BEBB11524D5135D00355C22 /* UIFontExtension.swift */,
325+
);
326+
path = UILabel;
327+
sourceTree = "<group>";
328+
};
313329
/* End PBXGroup section */
314330

315331
/* Begin PBXHeadersBuildPhase section */
@@ -633,8 +649,10 @@
633649
9B267BE424408357002F571E /* UITextViewExtension.swift in Sources */,
634650
9B267BE524408357002F571E /* UITextFieldExtension.swift in Sources */,
635651
9B267BE624408357002F571E /* UILabelExtension.swift in Sources */,
652+
9BEBB11424D511A000355C22 /* UILabelLayoutManagerDelegate.swift in Sources */,
636653
9B267BE724408357002F571E /* ShadowExtension.swift in Sources */,
637654
9B267BE824408357002F571E /* UIButtonExtension.swift in Sources */,
655+
9BEBB11724D5135D00355C22 /* UIFontExtension.swift in Sources */,
638656
9B8765A324850742009C51C2 /* ObjectExtension.swift in Sources */,
639657
9B267BB32440811F002F571E /* CGSizeExtension.swift in Sources */,
640658
9B267BB42440811F002F571E /* CGRectExtension.swift in Sources */,
@@ -688,8 +706,10 @@
688706
9B6E89E32382C392009EBEBE /* ParagraphStyle.swift in Sources */,
689707
9B6E89AA2382742F009EBEBE /* UITextFieldExtension.swift in Sources */,
690708
9B6E89E52382C488009EBEBE /* Attachment.swift in Sources */,
709+
9BEBB11324D511A000355C22 /* UILabelLayoutManagerDelegate.swift in Sources */,
691710
9B6E89A223826D8E009EBEBE /* Interpolation.swift in Sources */,
692711
9B6E89B3238276B0009EBEBE /* CGPointExtension.swift in Sources */,
712+
9BEBB11624D5135D00355C22 /* UIFontExtension.swift in Sources */,
693713
9B8765A024850742009C51C2 /* ObjectExtension.swift in Sources */,
694714
9B6E89DF23828F7C009EBEBE /* ShadowExtension.swift in Sources */,
695715
9B6E89AF238275D2009EBEBE /* CGSizeExtension.swift in Sources */,
@@ -1160,6 +1180,7 @@
11601180
PRODUCT_BUNDLE_IDENTIFIER = com.lee.attributedstring.ios;
11611181
PRODUCT_NAME = AttributedString;
11621182
SKIP_INSTALL = YES;
1183+
SWIFT_OBJC_BRIDGING_HEADER = "";
11631184
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
11641185
SWIFT_VERSION = 5.0;
11651186
TARGETED_DEVICE_FAMILY = "1,2";
@@ -1188,6 +1209,7 @@
11881209
PRODUCT_BUNDLE_IDENTIFIER = com.lee.attributedstring.ios;
11891210
PRODUCT_NAME = AttributedString;
11901211
SKIP_INSTALL = YES;
1212+
SWIFT_OBJC_BRIDGING_HEADER = "";
11911213
SWIFT_VERSION = 5.0;
11921214
TARGETED_DEVICE_FAMILY = "1,2";
11931215
};
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
//
2+
// UIFontExtension.swift
3+
// ┌─┐ ┌───────┐ ┌───────┐
4+
// │ │ │ ┌─────┘ │ ┌─────┘
5+
// │ │ │ └─────┐ │ └─────┐
6+
// │ │ │ ┌─────┘ │ ┌─────┘
7+
// │ └─────┐│ └─────┐ │ └─────┐
8+
// └───────┘└───────┘ └───────┘
9+
//
10+
// Created by Lee on 2020/8/1.
11+
// Copyright © 2020 LEE. All rights reserved.
12+
//
13+
14+
#if os(iOS) || os(tvOS)
15+
16+
import UIKit
17+
18+
private var UIFontSystemFontKey: Void?
19+
20+
extension UIFont {
21+
22+
static let Patch: Void = {
23+
swizzleMethod(for: UIFont.self, #selector(getter: lineHeight), #selector(getter: a_lineHeight))
24+
swizzleMethod(for: UIFont.self, #selector(getter: descender), #selector(getter: a_descender))
25+
swizzleMethod(for: UIFont.self, #selector(getter: leading), #selector(getter: a_leading))
26+
swizzleMethod(for: UIFont.self, #selector(getter: ascender), #selector(getter: a_ascender))
27+
swizzleMethod(for: UIFont.self, #selector(getter: capHeight), #selector(getter: a_capHeight))
28+
swizzleMethod(for: UIFont.self, #selector(getter: xHeight), #selector(getter: a_xHeight))
29+
} ()
30+
31+
private var systemFont: UIFont {
32+
return associated.get(&UIFontSystemFontKey) ?? {
33+
associated.set(assign: &UIFontSystemFontKey, $0); return $0
34+
} (UIFont.systemFont(ofSize: pointSize))
35+
}
36+
37+
@objc
38+
var a_lineHeight: CGFloat {
39+
systemFont.a_lineHeight
40+
}
41+
42+
@objc
43+
var a_descender: CGFloat {
44+
systemFont.a_descender
45+
}
46+
47+
@objc
48+
var a_leading: CGFloat {
49+
systemFont.a_leading
50+
}
51+
52+
@objc
53+
var a_ascender: CGFloat {
54+
systemFont.a_ascender
55+
}
56+
57+
@objc
58+
var a_capHeight: CGFloat {
59+
systemFont.a_capHeight
60+
}
61+
62+
@objc
63+
var a_xHeight: CGFloat {
64+
systemFont.a_xHeight
65+
}
66+
}
67+
68+
#endif

0 commit comments

Comments
 (0)