@@ -66,9 +66,7 @@ extension TextMessageLayoutProviderProtocol {
6666
6767public 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)
0 commit comments