@@ -24,7 +24,7 @@ extension MessageView: ConfigurableItem {
2424
2525 // MARK: - Model
2626
27- public struct Model : Equatable , AlignmentProvider {
27+ public struct Model : Equatable , AlignmentProvider , TextProvider {
2828
2929 // MARK: - Editor
3030
@@ -211,6 +211,34 @@ extension MessageView: ConfigurableItem {
211211
212212}
213213
214+ // MARK: - CalculatableHeightItem
215+
216+ extension MessageView : CalculatableHeightItem , FrameProvider {
217+
218+ public static func getHeight( forWidth width: CGFloat , with model: Model ) -> CGFloat {
219+ let constraintRect = CGSize ( width: width, height: . greatestFiniteMagnitude)
220+ let boundingBox = getFrame ( constraintRect: constraintRect, model: model)
221+ let height = ceil ( boundingBox. height)
222+
223+ return height
224+ }
225+
226+ }
227+
228+ // MARK: - CalculatableWidthItem
229+
230+ extension MessageView : CalculatableWidthItem {
231+
232+ public static func getWidth( forHeight height: CGFloat , with model: Model ) -> CGFloat {
233+ let constraintRect = CGSize ( width: . greatestFiniteMagnitude, height: height)
234+ let boundingBox = getFrame ( constraintRect: constraintRect, model: model)
235+ let width = ceil ( boundingBox. width)
236+
237+ return width
238+ }
239+
240+ }
241+
214242// MARK: - Private methods
215243
216244private extension MessageView {
@@ -269,5 +297,39 @@ extension MessageView: UITextViewDelegate {
269297 return false
270298 }
271299
300+ }
301+
302+ extension MessageView . Model {
303+
304+ public func getAttributes( ) -> [ NSAttributedString . Key : Any ] {
305+ switch text {
306+ case . string:
307+ let edgeInsets : UIEdgeInsets
308+ switch alignment {
309+ case . leading( let insets) :
310+ edgeInsets = insets
311+ case . trailing( let insets) :
312+ edgeInsets = insets
313+ case . all( let insets) :
314+ edgeInsets = insets
315+ }
316+ let paragraphStyle = NSMutableParagraphStyle ( )
317+ paragraphStyle. lineBreakMode = textLayout. lineBreakMode
318+ paragraphStyle. firstLineHeadIndent = edgeInsets. left
319+ paragraphStyle. headIndent = edgeInsets. right
320+ paragraphStyle. paragraphSpacingBefore = edgeInsets. top
321+ paragraphStyle. paragraphSpacing = edgeInsets. bottom
322+ paragraphStyle. alignment = textAlignment
323+
324+ var attributes : [ NSAttributedString . Key : Any ] = [ : ]
325+ attributes [ . font] = textStyle. font
326+ attributes [ . foregroundColor] = textStyle. color
327+ attributes [ . paragraphStyle] = paragraphStyle
328+ return attributes
329+ case . attributedString( let attributedText) :
330+ return attributedText. attributes ( at: 0 , effectiveRange: nil )
331+ }
332+ }
333+
272334}
273335#endif
0 commit comments