Skip to content

Commit fac85ff

Browse files
committed
Fix AttributedStringRepresentation
1 parent 6d6aabf commit fac85ff

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

Sources/Litext/LTXLabel/Attachments/LTXAttachment.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,10 @@ public class LTXAttachment {
1616
size = .zero
1717
}
1818

19-
// 获取 attachment 的文本表示
2019
public func attributedStringRepresentation() -> NSAttributedString {
2120
if let view = view as? LTXAttributeStringRepresentable {
2221
return view.attributedStringRepresentation()
2322
}
24-
// 如果没有实现协议,返回空格
2523
return NSAttributedString(string: " ")
2624
}
2725

Sources/Litext/LTXLabel/Selection/LTXLabel+Selection.swift

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,25 @@ extension LTXLabel {
8181
length: min(range.length, maxLen)
8282
)
8383

84-
return textLayout
84+
let selectedText = textLayout
8585
.attributedString
8686
.attributedSubstring(from: safeRange)
87+
88+
let mutableResult = NSMutableAttributedString(attributedString: selectedText)
89+
mutableResult.enumerateAttribute(
90+
LTXAttachmentAttributeName,
91+
in: NSRange(location: 0, length: mutableResult.length),
92+
options: []
93+
) { value, range, _ in
94+
if let attachment = value as? LTXAttachment {
95+
mutableResult.replaceCharacters(
96+
in: range,
97+
with: attachment.attributedStringRepresentation()
98+
)
99+
}
100+
}
101+
102+
return mutableResult
87103
}
88104

89105
func selectedPlainText() -> String? {

0 commit comments

Comments
 (0)