Skip to content

Commit 8f3a670

Browse files
committed
Make textContainerInset specify-able
1 parent 5b47591 commit 8f3a670

3 files changed

Lines changed: 29 additions & 24 deletions

File tree

Sources/ResizingTextView/ResizingTextView.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ public struct ResizingTextView: View, Equatable {
1818
#if os(macOS)
1919
var focusesNextKeyViewByTabKey: Bool = true
2020
var onInsertNewline: (() -> Bool)?
21+
var textContainerInset: CGSize?
2122
#elseif os(iOS)
2223
var autocapitalizationType: UITextAutocapitalizationType = .sentences
24+
var textContainerInset: UIEdgeInsets?
2325
#endif
2426

2527
#if os(macOS)
@@ -113,7 +115,8 @@ public struct ResizingTextView: View, Equatable {
113115
self.isFocused = false
114116
}
115117
},
116-
onInsertNewline: onInsertNewline
118+
onInsertNewline: onInsertNewline,
119+
textContainerInset: textContainerInset
117120
)
118121
.padding(.vertical, isEditable ? 8 : 0)
119122
.padding(.horizontal, isEditable ? 9 : 0)
@@ -136,7 +139,8 @@ public struct ResizingTextView: View, Equatable {
136139
font: font,
137140
canHaveNewLineCharacters: canHaveNewLineCharacters,
138141
foregroundColor: Color(foregroundColor),
139-
autocapitalizationType: autocapitalizationType)
142+
autocapitalizationType: autocapitalizationType,
143+
textContainerInset: textContainerInset)
140144
if let placeholder {
141145
Text(placeholder)
142146
.font(Font(font))

Sources/ResizingTextView/TextView (AppKit).swift

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,21 @@ struct TextView: NSViewRepresentable {
1818
var foregroundColor: Color
1919
var onFocusChanged: ((Bool) -> Void)?
2020
var onInsertNewline: (() -> Bool)?
21+
var textContainerInset: CGSize
2122

2223
init(_ text: Binding<String>,
23-
placeholder: String? = nil,
24-
isEditable: Bool = true,
25-
isScrollable: Bool = false,
26-
isSelectable: Bool = true,
27-
lineLimit: Int = 0,
24+
placeholder: String?,
25+
isEditable: Bool,
26+
isScrollable: Bool,
27+
isSelectable: Bool,
28+
lineLimit: Int,
2829
font: NSFont,
29-
canHaveNewLineCharacters: Bool = true,
30-
focusesNextKeyViewByTabKey: Bool = true,
31-
foregroundColor: Color = defaultForegroundColor,
32-
onFocusChanged: ((Bool) -> Void)? = nil,
33-
onInsertNewline: (() -> Bool)? = nil
30+
canHaveNewLineCharacters: Bool,
31+
focusesNextKeyViewByTabKey: Bool,
32+
foregroundColor: Color?,
33+
onFocusChanged: ((Bool) -> Void)?,
34+
onInsertNewline: (() -> Bool)?,
35+
textContainerInset: CGSize?
3436
) {
3537
self._text = text
3638
self.placeholder = placeholder
@@ -40,23 +42,16 @@ struct TextView: NSViewRepresentable {
4042
self.lineLimit = lineLimit
4143
self.canHaveNewLineCharacters = canHaveNewLineCharacters
4244
self.focusesNextKeyViewByTabKey = focusesNextKeyViewByTabKey
43-
self.foregroundColor = foregroundColor
45+
self.foregroundColor = foregroundColor ?? Self.defaultForegroundColor
4446
self.font = font
4547
self.onFocusChanged = onFocusChanged
4648
self.onInsertNewline = onInsertNewline
47-
}
48-
49-
init(text: String) {
50-
self.init(
51-
Binding<String>.constant(text),
52-
isEditable: false,
53-
font: NSFont.preferredFont(forTextStyle: .body),
54-
foregroundColor: Self.defaultForegroundColor)
49+
self.textContainerInset = textContainerInset ?? CGSize(width: -5, height: 0)
5550
}
5651

5752
func makeNSView(context: Context) -> TextEnclosingScrollView {
5853
let textView = CustomTextView()
59-
textView.textContainerInset = .init(width: -5, height: 0)
54+
textView.textContainerInset = textContainerInset
6055
textView.delegate = context.coordinator
6156
textView.isRichText = false
6257
textView.allowsUndo = true

Sources/ResizingTextView/TextView (UIKit).swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ struct TextView: UIViewRepresentable {
1717
private var canHaveNewLineCharacters: Bool
1818
private var width: CGFloat?
1919
private var autocapitalizationType: UITextAutocapitalizationType
20+
private var textContainerInset: UIEdgeInsets
2021

2122
init(_ text: Binding<String>,
2223
isEditable: Bool,
@@ -26,7 +27,8 @@ struct TextView: UIViewRepresentable {
2627
font: UIFont,
2728
canHaveNewLineCharacters: Bool,
2829
foregroundColor: Color,
29-
autocapitalizationType: UITextAutocapitalizationType) {
30+
autocapitalizationType: UITextAutocapitalizationType,
31+
textContainerInset: UIEdgeInsets?) {
3032
self._text = text
3133
self.isEditable = isEditable
3234
self.isScrollable = isScrollable
@@ -36,6 +38,10 @@ struct TextView: UIViewRepresentable {
3638
self.font = font
3739
self.canHaveNewLineCharacters = canHaveNewLineCharacters
3840
self.autocapitalizationType = autocapitalizationType
41+
42+
// 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.
43+
let defaultTextContainerInset = UIEdgeInsets(top: 0.00000001, left: 0.00000001, bottom: 0.00000001, right: 0.00000001)
44+
self.textContainerInset = textContainerInset ?? defaultTextContainerInset
3945
}
4046

4147
func makeUIView(context: Context) -> CustomTextView {
@@ -88,7 +94,7 @@ struct TextView: UIViewRepresentable {
8894
}
8995

9096
if !isEditable {
91-
view.textContainerInset = .zero
97+
view.textContainerInset = textContainerInset
9298
needsInvalidateIntrinsicContentSize = true
9399
}
94100

0 commit comments

Comments
 (0)