From 6dd1a91766ce5328241a33d806df34f81cd6837b Mon Sep 17 00:00:00 2001 From: kenmueller Date: Mon, 25 May 2020 15:03:57 -0700 Subject: [PATCH] Added text padding --- README.md | 8 +++++--- Sources/TextView/TextView.swift | 32 +++++++++++++++++++++++++++++--- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index cf5b98f..7c4acc6 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,8 @@ - You can also modify this value to automatically select and deselect the `TextView` - `placeholder: String? = nil` - `textAlignment: TextView.TextAlignment = .left` +- `textHorizontalPadding: CGFloat = 0` +- `textVerticalPadding: CGFloat = 7` - `placeholderAlignment: Alignment = .topLeading` - `placeholderHorizontalPadding: CGFloat = 4.5` - `placeholderVerticalPadding: CGFloat = 7` @@ -36,7 +38,7 @@ - `isScrollingEnabled: Bool = true` - `isUserInteractionEnabled: Bool = true` - `shouldWaitUntilCommit: Bool = true` - - For multi-stage input methods, setting this to `false` would make TextView completely unusable. + - For multi-stage input methods, setting this to `false` would make the `TextView` completely unusable. - This option will ignore text changes when the user is still composing characters. ## Example @@ -46,7 +48,7 @@ import SwiftUI import TextView struct ContentView: View { - @State var input = "" + @State var text = "" @State var isEditing = false var body: some View { @@ -57,7 +59,7 @@ struct ContentView: View { Text("\(isEditing ? "Stop" : "Start") editing") } TextView( - text: $input, + text: $text, isEditing: $isEditing, placeholder: "Enter text here" ) diff --git a/Sources/TextView/TextView.swift b/Sources/TextView/TextView.swift index f8154ed..e6070d0 100644 --- a/Sources/TextView/TextView.swift +++ b/Sources/TextView/TextView.swift @@ -35,6 +35,8 @@ public struct TextView: View { @Binding private var isEditing: Bool private let textAlignment: TextAlignment + private let textHorizontalPadding: CGFloat + private let textVerticalPadding: CGFloat private let font: UIFont private let textColor: UIColor private let backgroundColor: UIColor @@ -52,6 +54,8 @@ public struct TextView: View { text: Binding, isEditing: Binding, textAlignment: TextAlignment, + textHorizontalPadding: CGFloat, + textVerticalPadding: CGFloat, font: UIFont, textColor: UIColor, backgroundColor: UIColor, @@ -69,6 +73,8 @@ public struct TextView: View { _isEditing = isEditing self.textAlignment = textAlignment + self.textHorizontalPadding = textHorizontalPadding + self.textVerticalPadding = textVerticalPadding self.font = font self.textColor = textColor self.backgroundColor = backgroundColor @@ -96,12 +102,17 @@ public struct TextView: View { public func updateUIView(_ textView: UITextView, context _: Context) { if !shouldWaitUntilCommit || textView.markedTextRange == nil { let textViewWasEmpty = textView.text.isEmpty - let selectedRange = textView.selectedTextRange + let oldSelectedRange = textView.selectedTextRange + textView.text = text textView.selectedTextRange = textViewWasEmpty - ? textView.textRange(from: textView.endOfDocument, to: textView.endOfDocument) - : selectedRange + ? textView.textRange( + from: textView.endOfDocument, + to: textView.endOfDocument + ) + : oldSelectedRange } + textView.textAlignment = textAlignment textView.font = font textView.textColor = textColor @@ -115,6 +126,13 @@ public struct TextView: View { textView.isScrollEnabled = isScrollingEnabled textView.isUserInteractionEnabled = isUserInteractionEnabled + textView.textContainerInset = .init( + top: textVerticalPadding, + left: textHorizontalPadding, + bottom: textVerticalPadding, + right: textHorizontalPadding + ) + DispatchQueue.main.async { _ = self.isEditing ? textView.becomeFirstResponder() @@ -135,6 +153,8 @@ public struct TextView: View { private let placeholder: String? private let textAlignment: TextAlignment + private let textHorizontalPadding: CGFloat + private let textVerticalPadding: CGFloat private let placeholderAlignment: Alignment private let placeholderHorizontalPadding: CGFloat private let placeholderVerticalPadding: CGFloat @@ -157,6 +177,8 @@ public struct TextView: View { isEditing: Binding, placeholder: String? = nil, textAlignment: TextAlignment = .left, + textHorizontalPadding: CGFloat = 0, + textVerticalPadding: CGFloat = 7, placeholderAlignment: Alignment = .topLeading, placeholderHorizontalPadding: CGFloat = 4.5, placeholderVerticalPadding: CGFloat = 7, @@ -179,6 +201,8 @@ public struct TextView: View { self.placeholder = placeholder self.textAlignment = textAlignment + self.textHorizontalPadding = textHorizontalPadding + self.textVerticalPadding = textVerticalPadding self.placeholderAlignment = placeholderAlignment self.placeholderHorizontalPadding = placeholderHorizontalPadding self.placeholderVerticalPadding = placeholderVerticalPadding @@ -206,6 +230,8 @@ public struct TextView: View { text: $text, isEditing: $isEditing, textAlignment: textAlignment, + textHorizontalPadding: textHorizontalPadding, + textVerticalPadding: textVerticalPadding, font: font, textColor: textColor, backgroundColor: backgroundColor,