Skip to content

Commit 380097b

Browse files
committed
(iOS) Fix that the vertical paddings are 0. It should be 8 by default
1 parent 33d2d34 commit 380097b

2 files changed

Lines changed: 13 additions & 11 deletions

File tree

Sources/ResizingTextView/ResizingTextView.swift

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,11 @@ public struct ResizingTextView: View, Equatable {
138138
.opacity(isFocused && isEditable ? 1 : 0).scaleEffect(isFocused && isEditable ? 1 : 1.03))
139139
#elseif os(iOS)
140140
ZStack(alignment: .topLeading) {
141+
142+
/// 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 0.00...1 value to the insets.
143+
let defaultTextContainerInset = UIEdgeInsets(top: 8, left: 0.00000001, bottom: 8, right: 0.00000001)
144+
let effectiveTextContainerInset = textContainerInset ?? defaultTextContainerInset
145+
141146
TextView(
142147
$text,
143148
isEditable: isEditable,
@@ -148,18 +153,18 @@ public struct ResizingTextView: View, Equatable {
148153
canHaveNewLineCharacters: canHaveNewLineCharacters,
149154
foregroundColor: Color(foregroundColor),
150155
autocapitalizationType: autocapitalizationType,
151-
textContainerInset: textContainerInset
156+
textContainerInset: effectiveTextContainerInset
152157
)
153158
if let placeholder {
154159
let isLTR = layoutDirection == .leftToRight
155160
Text(placeholder)
156161
.font(Font(font))
157162
.lineLimit(1)
158163
.foregroundColor(Color(foregroundColor.withAlphaComponent(0.2)))
159-
.padding(.top, textContainerInset?.top ?? 0)
160-
.padding(isLTR ? .leading : .trailing, textContainerInset?.left ?? 0)
161-
.padding(.bottom, textContainerInset?.bottom ?? 0)
162-
.padding(isLTR ? .trailing : .leading, textContainerInset?.right ?? 0)
164+
.padding(.top, effectiveTextContainerInset.top)
165+
.padding(isLTR ? .leading : .trailing, effectiveTextContainerInset.left)
166+
.padding(.bottom, effectiveTextContainerInset.bottom)
167+
.padding(isLTR ? .trailing : .leading, effectiveTextContainerInset.right)
163168
.allowsHitTesting(false)
164169
.opacity(text.isEmpty ? 1 : 0)
165170
}

Sources/ResizingTextView/TextView (UIKit).swift

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ import UIKit
77
struct TextView: UIViewRepresentable {
88
static let defaultForegroundColor = Color(UIColor.label)
99

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)
12-
1310
@Binding private var text: String
1411
private var isEditable: Bool
1512
private var isScrollable: Bool
@@ -20,7 +17,7 @@ struct TextView: UIViewRepresentable {
2017
private var canHaveNewLineCharacters: Bool
2118
private var width: CGFloat?
2219
private var autocapitalizationType: UITextAutocapitalizationType
23-
private var textContainerInset: UIEdgeInsets?
20+
private var textContainerInset: UIEdgeInsets
2421

2522
init(
2623
_ text: Binding<String>,
@@ -32,7 +29,7 @@ struct TextView: UIViewRepresentable {
3229
canHaveNewLineCharacters: Bool,
3330
foregroundColor: Color,
3431
autocapitalizationType: UITextAutocapitalizationType,
35-
textContainerInset: UIEdgeInsets?
32+
textContainerInset: UIEdgeInsets
3633
) {
3734
_text = text
3835
self.isEditable = isEditable
@@ -84,7 +81,7 @@ struct TextView: UIViewRepresentable {
8481
if view.autocapitalizationType != autocapitalizationType {
8582
view.autocapitalizationType = autocapitalizationType
8683
}
87-
let textContainerInset = textContainerInset ?? Self.defaultTextContainerInset
84+
let textContainerInset = textContainerInset
8885
if view.textContainerInset != textContainerInset {
8986
view.textContainerInset = textContainerInset
9087
}

0 commit comments

Comments
 (0)