Skip to content

Commit 9df0ead

Browse files
committed
优化内部变量命名, 修复UITextView添加视图附件大小不正确问题
1 parent 7d42073 commit 9df0ead

6 files changed

Lines changed: 63 additions & 33 deletions

File tree

AttributedString.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Pod::Spec.new do |s|
22

33
s.name = "AttributedString"
4-
s.version = "1.6.6"
4+
s.version = "1.6.7"
55
s.summary = "基于Swift字符串插值快速构建你想要的富文本, 支持点击按住等事件获取, 支持多种类型过滤"
66

77
s.homepage = "https://github.com/lixiang1994/AttributedString"

AttributedString.xcodeproj/project.pbxproj

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -838,7 +838,7 @@
838838
"@executable_path/Frameworks",
839839
"@loader_path/Frameworks",
840840
);
841-
MARKETING_VERSION = 1.6.6;
841+
MARKETING_VERSION = 1.6.7;
842842
PRODUCT_BUNDLE_IDENTIFIER = com.attributedstring.watchos;
843843
PRODUCT_NAME = AttributedString;
844844
SDKROOT = watchos;
@@ -866,7 +866,7 @@
866866
"@executable_path/Frameworks",
867867
"@loader_path/Frameworks",
868868
);
869-
MARKETING_VERSION = 1.6.6;
869+
MARKETING_VERSION = 1.6.7;
870870
PRODUCT_BUNDLE_IDENTIFIER = com.attributedstring.watchos;
871871
PRODUCT_NAME = AttributedString;
872872
SDKROOT = watchos;
@@ -893,7 +893,7 @@
893893
"@executable_path/Frameworks",
894894
"@loader_path/Frameworks",
895895
);
896-
MARKETING_VERSION = 1.6.6;
896+
MARKETING_VERSION = 1.6.7;
897897
PRODUCT_BUNDLE_IDENTIFIER = com.attributedstring.tvos;
898898
PRODUCT_NAME = AttributedString;
899899
SDKROOT = appletvos;
@@ -920,7 +920,7 @@
920920
"@executable_path/Frameworks",
921921
"@loader_path/Frameworks",
922922
);
923-
MARKETING_VERSION = 1.6.6;
923+
MARKETING_VERSION = 1.6.7;
924924
PRODUCT_BUNDLE_IDENTIFIER = com.attributedstring.tvos;
925925
PRODUCT_NAME = AttributedString;
926926
SDKROOT = appletvos;
@@ -996,7 +996,7 @@
996996
"@loader_path/Frameworks",
997997
);
998998
MACOSX_DEPLOYMENT_TARGET = 10.13;
999-
MARKETING_VERSION = 1.6.6;
999+
MARKETING_VERSION = 1.6.7;
10001000
PRODUCT_BUNDLE_IDENTIFIER = com.lee.attributedstring.macos;
10011001
PRODUCT_NAME = AttributedString;
10021002
SDKROOT = macosx;
@@ -1023,7 +1023,7 @@
10231023
"@loader_path/Frameworks",
10241024
);
10251025
MACOSX_DEPLOYMENT_TARGET = 10.13;
1026-
MARKETING_VERSION = 1.6.6;
1026+
MARKETING_VERSION = 1.6.7;
10271027
PRODUCT_BUNDLE_IDENTIFIER = com.lee.attributedstring.macos;
10281028
PRODUCT_NAME = AttributedString;
10291029
SDKROOT = macosx;
@@ -1170,7 +1170,7 @@
11701170
"@executable_path/Frameworks",
11711171
"@loader_path/Frameworks",
11721172
);
1173-
MARKETING_VERSION = 1.6.6;
1173+
MARKETING_VERSION = 1.6.7;
11741174
PRODUCT_BUNDLE_IDENTIFIER = com.lee.attributedstring.ios;
11751175
PRODUCT_NAME = AttributedString;
11761176
SKIP_INSTALL = YES;
@@ -1199,7 +1199,7 @@
11991199
"@executable_path/Frameworks",
12001200
"@loader_path/Frameworks",
12011201
);
1202-
MARKETING_VERSION = 1.6.6;
1202+
MARKETING_VERSION = 1.6.7;
12031203
PRODUCT_BUNDLE_IDENTIFIER = com.lee.attributedstring.ios;
12041204
PRODUCT_NAME = AttributedString;
12051205
SKIP_INSTALL = YES;

Demo/Demo.xcodeproj/project.xcworkspace/contents.xcworkspacedata

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>IDEDidComputeMac32BitWarning</key>
6+
<true/>
7+
</dict>
8+
</plist>

Sources/Attachment.swift

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,15 @@ extension AttributedString {
8686

8787
switch style.mode {
8888
case .proposed:
89-
let radio = image.size.width / image.size.height
90-
let width = min(lineFrag.height * radio, lineFrag.width)
91-
let height = width / radio
89+
let ratio = image.size.width / image.size.height
90+
let width = min(lineFrag.height * ratio, lineFrag.width)
91+
let height = width / ratio
9292
return .init(point(.init(width, height)), .init(width, height))
9393

9494
case .original:
95-
let radio = image.size.width / image.size.height
95+
let ratio = image.size.width / image.size.height
9696
let width = min(image.size.width, lineFrag.width)
97-
let height = width / radio
97+
let height = width / ratio
9898
return .init(point(.init(width, height)), .init(width, height))
9999

100100
case .custom(let size):
@@ -110,7 +110,7 @@ extension AttributedString {
110110
public typealias Style = Attachment.Style
111111

112112
let view: UIView
113-
private let style: Style
113+
let style: Style
114114

115115
/// Custom View (Only support UITextView)
116116
/// - Parameter view: 视图
@@ -148,15 +148,15 @@ extension AttributedString {
148148

149149
switch style.mode {
150150
case .proposed:
151-
let radio = view.bounds.width / view.bounds.height
152-
let width = min(lineFrag.height * radio, lineFrag.width)
153-
let height = width / radio
151+
let ratio = view.bounds.width / view.bounds.height
152+
let width = min(lineFrag.height * ratio, lineFrag.width)
153+
let height = width / ratio
154154
return .init(point(.init(width, height)), .init(width, height))
155155

156156
case .original:
157-
let radio = view.bounds.width / view.bounds.height
157+
let ratio = view.bounds.width / view.bounds.height
158158
let width = min(view.bounds.width, lineFrag.width)
159-
let height = width / radio
159+
let height = width / ratio
160160
return .init(point(.init(width, height)), .init(width, height))
161161

162162
case .custom(let size):
@@ -176,8 +176,6 @@ extension AttributedString.Attachment {
176176

177177
case origin // Baseline
178178

179-
case bottom // Bottom
180-
181179
case offset(CGPoint)
182180
}
183181

@@ -189,8 +187,8 @@ extension AttributedString.Attachment {
189187
case custom(CGSize)
190188
}
191189

192-
fileprivate let mode: Mode
193-
fileprivate let alignment: Alignment
190+
let mode: Mode
191+
let alignment: Alignment
194192

195193
/// 建议的大小 (一般为当前行的高度)
196194
/// - Parameter alignment: 对齐方式
@@ -269,9 +267,6 @@ fileprivate extension AttributedString.Attachment.Alignment {
269267
font = .systemFont(ofSize: fontSize)
270268

271269
switch self {
272-
case .bottom:
273-
return .zero
274-
275270
case .origin:
276271
return .init(0, font.descender)
277272

Sources/Extension/UIKit/UITextViewExtension.swift

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ extension AttributedStringWrapper where Base: UITextView {
206206

207207
// 添加子视图
208208
attachments.forEach {
209-
let view = AttachmentView($0.value.view)
209+
let view = AttachmentView($0.value.view, with: $0.value.style)
210210
base.addSubview(view)
211211
base.attachmentViews[$0.key] = view
212212
}
@@ -399,12 +399,16 @@ fileprivate extension UITextView {
399399
/// 附件视图
400400
private class AttachmentView: UIView {
401401

402+
typealias Style = AttributedString.Attachment.Style
403+
402404
let view: UIView
405+
let style: Style
403406

404407
private var observation: [String: NSKeyValueObservation] = [:]
405408

406-
init(_ view: UIView) {
409+
init(_ view: UIView, with style: Style) {
407410
self.view = view
411+
self.style = style
408412
super.init(frame: view.bounds)
409413

410414
clipsToBounds = true
@@ -436,10 +440,26 @@ private class AttachmentView: UIView {
436440
CATransaction.begin()
437441
CATransaction.setDisableActions(true)
438442
view.center = .init(bounds.width * 0.5, bounds.height * 0.5)
439-
view.transform = .init(
440-
scaleX: bounds.width / view.bounds.width,
441-
y: bounds.height / view.bounds.height
442-
)
443+
switch style.mode {
444+
case .proposed:
445+
view.transform = .init(
446+
scaleX: bounds.width / view.bounds.width,
447+
y: bounds.height / view.bounds.height
448+
)
449+
450+
case .original:
451+
let ratio = view.bounds.width / view.bounds.height
452+
view.transform = .init(
453+
scaleX: bounds.width / view.bounds.width,
454+
y: bounds.width / view.bounds.width / ratio
455+
)
456+
457+
case .custom(let size):
458+
view.transform = .init(
459+
scaleX: size.width / view.bounds.width,
460+
y: size.height / view.bounds.height
461+
)
462+
}
443463
CATransaction.commit()
444464
}
445465
}

0 commit comments

Comments
 (0)