From 2858ca7e8f535513a00d63d0cd3b0682acf2584e Mon Sep 17 00:00:00 2001 From: Raul Menezes Date: Thu, 24 Oct 2024 22:08:10 +0100 Subject: [PATCH 1/2] Add option to pass original NSAttributedString instead of creating a internal one for text calculation --- .../Content/MessageManualLayoutProvider.swift | 44 ++++++++++++------- 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/ChattoAdditions/sources/Chat Items/CompoundMessage/Content/MessageManualLayoutProvider.swift b/ChattoAdditions/sources/Chat Items/CompoundMessage/Content/MessageManualLayoutProvider.swift index 895817ac..74f669b8 100644 --- a/ChattoAdditions/sources/Chat Items/CompoundMessage/Content/MessageManualLayoutProvider.swift +++ b/ChattoAdditions/sources/Chat Items/CompoundMessage/Content/MessageManualLayoutProvider.swift @@ -66,9 +66,7 @@ extension TextMessageLayoutProviderProtocol { public struct TextMessageLayoutProvider: Hashable, TextMessageLayoutProviderProtocol { - private let text: String - private let font: UIFont - private let paragraphStyle: NSParagraphStyle? + private let text: NSAttributedString private let textInsets: UIEdgeInsets private let textInsetsFromSafeArea: UIEdgeInsets? private let numberOfLines: Int @@ -79,9 +77,33 @@ public struct TextMessageLayoutProvider: Hashable, TextMessageLayoutProviderProt textInsets: UIEdgeInsets, textInsetsFromSafeArea: UIEdgeInsets? = nil, numberOfLines: Int = 0) { - self.text = text - self.font = font - self.paragraphStyle = paragraphStyle + + var attributes: [NSAttributedString.Key: Any] = [ + NSAttributedString.Key.font: font, + // See https://github.com/badoo/Chatto/issues/129 + NSAttributedString.Key(rawValue: "NSOriginalFont"): font, + ] + + if let paragraphStyle { + attributes[.paragraphStyle] = paragraphStyle + } + + self.init( + attributedString: NSAttributedString( + string: text, + attributes: attributes + ), + textInsets: textInsets, + textInsetsFromSafeArea: textInsetsFromSafeArea, + numberOfLines: numberOfLines + ) + } + + public init(attributedString: NSAttributedString, + textInsets: UIEdgeInsets, + textInsetsFromSafeArea: UIEdgeInsets? = nil, + numberOfLines: Int = 0) { + self.text = attributedString self.textInsets = textInsets self.textInsetsFromSafeArea = textInsetsFromSafeArea self.numberOfLines = numberOfLines @@ -97,15 +119,7 @@ public struct TextMessageLayoutProvider: Hashable, TextMessageLayoutProviderProt textContainer.lineFragmentPadding = 0 textContainer.maximumNumberOfLines = self.numberOfLines - // See https://github.com/badoo/Chatto/issues/129 - var textAttributes: [NSAttributedString.Key : Any] = [ - NSAttributedString.Key.font: self.font, - NSAttributedString.Key(rawValue: "NSOriginalFont"): self.font - ] - if let paragraphStyle = self.paragraphStyle { - textAttributes[.paragraphStyle] = paragraphStyle - } - let textStorage = NSTextStorage(string: self.text, attributes: textAttributes) + let textStorage = NSTextStorage(attributedString: self.text) let layoutManager = NSLayoutManager() layoutManager.addTextContainer(textContainer) From 6651359a35283d78766f1602f6d8bd616dda4b83 Mon Sep 17 00:00:00 2001 From: Raul Menezes Date: Thu, 24 Oct 2024 23:32:40 +0100 Subject: [PATCH 2/2] Bumped simulator to fix CI tests --- scripts/test.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/test.sh b/scripts/test.sh index 83407196..21c7c058 100755 --- a/scripts/test.sh +++ b/scripts/test.sh @@ -23,5 +23,5 @@ xcodebuild clean build test \ -workspace $WORKSPACE \ -scheme $SCHEME \ -sdk iphonesimulator \ - -destination 'platform=iOS Simulator,name=iPhone 11' \ + -destination 'platform=iOS Simulator,name=iPhone 15' \ -configuration Debug | xcpretty