@@ -6,6 +6,9 @@ import UIKit
66
77struct TextView : UIViewRepresentable {
88 static let defaultForegroundColor = Color ( UIColor . label)
9+
10+ /// HACK: In iOS 17, the last sentence of a non-editable text may not be drawn if the textContainerInset is `.zero`. To avoid it, we add this default value to the insets.
11+ private static let defaultTextContainerInset = UIEdgeInsets ( top: 0.00000001 , left: 0.00000001 , bottom: 0.00000001 , right: 0.00000001 )
912
1013 @Binding private var text : String
1114 private var isEditable : Bool
@@ -17,7 +20,7 @@ struct TextView: UIViewRepresentable {
1720 private var canHaveNewLineCharacters : Bool
1821 private var width : CGFloat ?
1922 private var autocapitalizationType : UITextAutocapitalizationType
20- private var textContainerInset : UIEdgeInsets
23+ private var textContainerInset : UIEdgeInsets ?
2124
2225 init (
2326 _ text: Binding < String > ,
@@ -31,7 +34,7 @@ struct TextView: UIViewRepresentable {
3134 autocapitalizationType: UITextAutocapitalizationType ,
3235 textContainerInset: UIEdgeInsets ?
3336 ) {
34- self . _text = text
37+ _text = text
3538 self . isEditable = isEditable
3639 self . isScrollable = isScrollable
3740 self . isSelectable = isSelectable
@@ -40,10 +43,7 @@ struct TextView: UIViewRepresentable {
4043 self . font = font
4144 self . canHaveNewLineCharacters = canHaveNewLineCharacters
4245 self . autocapitalizationType = autocapitalizationType
43-
44- // HACK: In iOS 17, the last sentence of a non-editable text may not be drawn if the textContainerInset is `.zero`. To avoid it, we add this default value to the insets.
45- let defaultTextContainerInset = UIEdgeInsets ( top: 0.00000001 , left: 0.00000001 , bottom: 0.00000001 , right: 0.00000001 )
46- self . textContainerInset = textContainerInset ?? defaultTextContainerInset
46+ self . textContainerInset = textContainerInset
4747 }
4848
4949 func makeUIView( context: Context ) -> CustomTextView {
@@ -84,6 +84,10 @@ struct TextView: UIViewRepresentable {
8484 if view. autocapitalizationType != autocapitalizationType {
8585 view. autocapitalizationType = autocapitalizationType
8686 }
87+ let textContainerInset = textContainerInset ?? Self . defaultTextContainerInset
88+ if view. textContainerInset != textContainerInset {
89+ view. textContainerInset = textContainerInset
90+ }
8791 if lineLimit > 0 {
8892 if view. textContainer. lineBreakMode != . byTruncatingTail {
8993 view. textContainer. lineBreakMode = . byTruncatingTail
0 commit comments