Skip to content

Commit

Permalink
Added text padding
Browse files Browse the repository at this point in the history
  • Loading branch information
kenmueller committed May 25, 2020
1 parent f41e24e commit 6dd1a91
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -57,7 +59,7 @@ struct ContentView: View {
Text("\(isEditing ? "Stop" : "Start") editing")
}
TextView(
text: $input,
text: $text,
isEditing: $isEditing,
placeholder: "Enter text here"
)
Expand Down
32 changes: 29 additions & 3 deletions Sources/TextView/TextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -52,6 +54,8 @@ public struct TextView: View {
text: Binding<String>,
isEditing: Binding<Bool>,
textAlignment: TextAlignment,
textHorizontalPadding: CGFloat,
textVerticalPadding: CGFloat,
font: UIFont,
textColor: UIColor,
backgroundColor: UIColor,
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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()
Expand All @@ -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
Expand All @@ -157,6 +177,8 @@ public struct TextView: View {
isEditing: Binding<Bool>,
placeholder: String? = nil,
textAlignment: TextAlignment = .left,
textHorizontalPadding: CGFloat = 0,
textVerticalPadding: CGFloat = 7,
placeholderAlignment: Alignment = .topLeading,
placeholderHorizontalPadding: CGFloat = 4.5,
placeholderVerticalPadding: CGFloat = 7,
Expand All @@ -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
Expand Down Expand Up @@ -206,6 +230,8 @@ public struct TextView: View {
text: $text,
isEditing: $isEditing,
textAlignment: textAlignment,
textHorizontalPadding: textHorizontalPadding,
textVerticalPadding: textVerticalPadding,
font: font,
textColor: textColor,
backgroundColor: backgroundColor,
Expand Down

0 comments on commit 6dd1a91

Please sign in to comment.