Skip to content

Commit fcbcbad

Browse files
authored
Merge pull request #794 from rmenezes/wrong_text_calculation_attributed_string
Wrong text calculation on AttributedString
2 parents 28e0eaa + 6651359 commit fcbcbad

File tree

2 files changed

+30
-16
lines changed

2 files changed

+30
-16
lines changed

ChattoAdditions/sources/Chat Items/CompoundMessage/Content/MessageManualLayoutProvider.swift

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,7 @@ extension TextMessageLayoutProviderProtocol {
6666

6767
public struct TextMessageLayoutProvider: Hashable, TextMessageLayoutProviderProtocol {
6868

69-
private let text: String
70-
private let font: UIFont
71-
private let paragraphStyle: NSParagraphStyle?
69+
private let text: NSAttributedString
7270
private let textInsets: UIEdgeInsets
7371
private let textInsetsFromSafeArea: UIEdgeInsets?
7472
private let numberOfLines: Int
@@ -79,9 +77,33 @@ public struct TextMessageLayoutProvider: Hashable, TextMessageLayoutProviderProt
7977
textInsets: UIEdgeInsets,
8078
textInsetsFromSafeArea: UIEdgeInsets? = nil,
8179
numberOfLines: Int = 0) {
82-
self.text = text
83-
self.font = font
84-
self.paragraphStyle = paragraphStyle
80+
81+
var attributes: [NSAttributedString.Key: Any] = [
82+
NSAttributedString.Key.font: font,
83+
// See https://github.com/badoo/Chatto/issues/129
84+
NSAttributedString.Key(rawValue: "NSOriginalFont"): font,
85+
]
86+
87+
if let paragraphStyle {
88+
attributes[.paragraphStyle] = paragraphStyle
89+
}
90+
91+
self.init(
92+
attributedString: NSAttributedString(
93+
string: text,
94+
attributes: attributes
95+
),
96+
textInsets: textInsets,
97+
textInsetsFromSafeArea: textInsetsFromSafeArea,
98+
numberOfLines: numberOfLines
99+
)
100+
}
101+
102+
public init(attributedString: NSAttributedString,
103+
textInsets: UIEdgeInsets,
104+
textInsetsFromSafeArea: UIEdgeInsets? = nil,
105+
numberOfLines: Int = 0) {
106+
self.text = attributedString
85107
self.textInsets = textInsets
86108
self.textInsetsFromSafeArea = textInsetsFromSafeArea
87109
self.numberOfLines = numberOfLines
@@ -97,15 +119,7 @@ public struct TextMessageLayoutProvider: Hashable, TextMessageLayoutProviderProt
97119
textContainer.lineFragmentPadding = 0
98120
textContainer.maximumNumberOfLines = self.numberOfLines
99121

100-
// See https://github.com/badoo/Chatto/issues/129
101-
var textAttributes: [NSAttributedString.Key : Any] = [
102-
NSAttributedString.Key.font: self.font,
103-
NSAttributedString.Key(rawValue: "NSOriginalFont"): self.font
104-
]
105-
if let paragraphStyle = self.paragraphStyle {
106-
textAttributes[.paragraphStyle] = paragraphStyle
107-
}
108-
let textStorage = NSTextStorage(string: self.text, attributes: textAttributes)
122+
let textStorage = NSTextStorage(attributedString: self.text)
109123

110124
let layoutManager = NSLayoutManager()
111125
layoutManager.addTextContainer(textContainer)

scripts/test.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@ xcodebuild clean build test \
2323
-workspace $WORKSPACE \
2424
-scheme $SCHEME \
2525
-sdk iphonesimulator \
26-
-destination 'platform=iOS Simulator,name=iPhone 11' \
26+
-destination 'platform=iOS Simulator,name=iPhone 15' \
2727
-configuration Debug | xcpretty

0 commit comments

Comments
 (0)