Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 42 additions & 26 deletions Sources/TextView/TextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public struct TextView: View {
private let textVerticalPadding: CGFloat
private let font: UIFont
private let textColor: UIColor
private let backgroundColor: UIColor
private let returnType: UIReturnKeyType
private let contentType: ContentType?
private let autocorrection: Autocorrection
Expand All @@ -65,7 +64,6 @@ public struct TextView: View {
textVerticalPadding: CGFloat,
font: UIFont,
textColor: UIColor,
backgroundColor: UIColor,
returnType: UIReturnKeyType,
contentType: ContentType?,
autocorrection: Autocorrection,
Expand All @@ -87,7 +85,6 @@ public struct TextView: View {
self.textVerticalPadding = textVerticalPadding
self.font = font
self.textColor = textColor
self.backgroundColor = backgroundColor
self.returnType = returnType
self.contentType = contentType
self.autocorrection = autocorrection
Expand All @@ -109,6 +106,7 @@ public struct TextView: View {
public func makeUIView(context: Context) -> UITextView {
let textView = UITextView()
textView.delegate = context.coordinator
textView.backgroundColor = nil
return textView
}

Expand All @@ -129,7 +127,6 @@ public struct TextView: View {
textView.textAlignment = textAlignment
textView.font = font
textView.textColor = textColor
textView.backgroundColor = backgroundColor
textView.returnKeyType = returnType
textView.textContentType = contentType
textView.autocorrectionType = autocorrection
Expand Down Expand Up @@ -246,10 +243,6 @@ public struct TextView: View {
self.shouldChange = shouldChange
}

private var _placeholder: String? {
text.isEmpty ? placeholder : nil
}

private var representable: Representable {
.init(
text: $text,
Expand All @@ -259,7 +252,6 @@ public struct TextView: View {
textVerticalPadding: textVerticalPadding,
font: font,
textColor: textColor,
backgroundColor: backgroundColor,
returnType: returnType,
contentType: contentType,
autocorrection: autocorrection,
Expand All @@ -275,28 +267,52 @@ public struct TextView: View {
)
}

@ViewBuilder
private var placeholderView: some View {
if let placeholder = text.isEmpty ? placeholder : nil {
Text(placeholder)
.font(.init(font))
.foregroundColor(placeholderColor)
.padding(.horizontal, placeholderHorizontalPadding)
.padding(.vertical, placeholderVerticalPadding)
}
}

public var body: some View {
GeometryReader { geometry in
ZStack {
self.representable
self._placeholder.map { placeholder in
Text(placeholder)
.font(.init(self.font))
.foregroundColor(self.placeholderColor)
.padding(.horizontal, self.placeholderHorizontalPadding)
.padding(.vertical, self.placeholderVerticalPadding)
.frame(
width: geometry.size.width,
height: geometry.size.height,
alignment: self.placeholderAlignment
)
.onTapGesture {
self.isEditing = true
}
representable
.background(placeholderView, alignment: placeholderAlignment)
.background(Color(backgroundColor))
}
}

struct TextView_Previews: PreviewProvider {
private struct PreviewView: View {
@State private var text: String = ""
@State private var isEditing: Bool = false

var body: some View {
VStack {
Button(action: { isEditing.toggle() }) {
Text("\(isEditing ? "Stop" : "Start") editing")
.padding()
.background(Color.white)
}
TextView(
text: $text,
isEditing: $isEditing,
placeholder: "Enter text here",
textAlignment: .natural,
backgroundColor: .systemBackground
)
}
}
}

static var previews: some View {
PreviewView()
.padding()
.background(Color.secondary.edgesIgnoringSafeArea(.all))
}
}

#endif