11import Foundation
22import UIKit
33import ComponentFlow
4- import MultilineTextComponent
54
65final class TextView : UIView {
76 private struct Params : Equatable {
@@ -22,8 +21,6 @@ final class TextView: UIView {
2221 private var layoutState : LayoutState ?
2322 private var animateContentsTransition : Bool = false
2423
25- private let content = ComponentView < Empty > ( )
26-
2724 override init ( frame: CGRect ) {
2825 super. init ( frame: CGRect ( ) )
2926
@@ -59,25 +56,17 @@ final class TextView: UIView {
5956 font = UIFont . systemFont ( ofSize: fontSize, weight: UIFont . Weight ( fontWeight) )
6057 }
6158
59+ let paragraphStyle = NSMutableParagraphStyle ( )
60+ paragraphStyle. alignment = alignment
61+ paragraphStyle. lineSpacing = 0.6
6262 let attributedString = NSAttributedString ( string: string, attributes: [
6363 . font: font,
6464 . foregroundColor: color,
65+ . paragraphStyle: paragraphStyle
6566 ] )
66-
67- let contentSize = self . content. update (
68- transition: . immediate,
69- component: AnyComponent ( MultilineTextComponent (
70- text: . plain( attributedString) ,
71- horizontalAlignment: alignment
72- ) ) ,
73- environment: { } ,
74- containerSize: CGSize ( width: constrainedWidth, height: 1000.0 )
75- )
76- if let contentView = self . content. view {
77- contentView. frame = CGRect ( origin: CGPoint ( ) , size: contentSize)
78- }
79-
80- let size = CGSize ( width: min ( constrainedWidth, contentSize. width) , height: contentSize. height)
67+ let stringBounds = attributedString. boundingRect ( with: CGSize ( width: constrainedWidth, height: 200.0 ) , options: . usesLineFragmentOrigin, context: nil )
68+ let stringSize = CGSize ( width: ceil ( stringBounds. width) , height: ceil ( stringBounds. height) )
69+ let size = CGSize ( width: min ( constrainedWidth, stringSize. width) , height: stringSize. height)
8170
8271 let layoutState = LayoutState ( params: params, size: size, attributedString: attributedString)
8372 if self . layoutState != layoutState {
@@ -90,12 +79,10 @@ final class TextView: UIView {
9079 }
9180
9281 override func draw( _ rect: CGRect ) {
93- guard let _ = self . layoutState else {
82+ guard let layoutState = self . layoutState else {
9483 return
9584 }
9685
97- if let contentView = self . content. view {
98- contentView. draw ( contentView. bounds)
99- }
86+ layoutState. attributedString. draw ( with: rect, options: [ . truncatesLastVisibleLine, . usesLineFragmentOrigin] , context: nil )
10087 }
10188}
0 commit comments