Greetings, traveler!
It may come as a surprise, but Apple has yet to provide a built-in solution for creating a placeholder in SwiftUI TextEditor. However, creating a custom view for this purpose is not difficult. Let’s explore some of the many ways we can solve this problem.
First, we need to create a custom View and give it a name. Inside the view, we’ll create two properties: one for the text and the other for the placeholder.
import SwiftUI
public struct TextEditorWithPlaceholder: View {
@State private var text = ""
private let placeholder: String
public init(
text: String = "",
placeholder: String
) {
self.text = text
self.placeholder = placeholder
}
public var body: some View {
EmtyView()
}
}
Next, we’ll place the TextEditor inside the View itself, and we’ll create an overlay for the placeholder using simple text. We’ll align the overlay so that it looks like a native placeholder and add the desired color. Finally, we’ll use the allowsHitTesting modifier to prevent user taps from interacting with the Text View.
import SwiftUI
public struct TextEditorWithPlaceholder: View {
@State private var text = ""
private let placeholder: String
public init(
text: String = "",
placeholder: String
) {
self.text = text
self.placeholder = placeholder
}
public var body: some View {
TextEditor(text: $text)
.overlay(alignment: .topLeading) {
Text(placeholder)
.foregroundColor(Color(uiColor: .placeholderText))
.multilineTextAlignment(.leading)
.opacity(text.isEmpty ? 1 : 0)
.padding(.vertical, 8)
.padding(.horizontal, 4)
.allowsHitTesting(false)
}
}
}
This small tutorial will help you solve an urgent problem quickly and quite universally. The source code is on GitHub. See you soon!
Check out other posts:
- Modular Form Validation in SwiftUI
- Simplifying Focus Management in SwiftUI with a Custom ViewModifier
- InlineArray – A Fixed-Size Arrays in Swift
- Weak let for Safer Immutable References
- Observing Value Changes with Observations in Swift 6.2
- Making String Interpolation Smarter in Swift 6.2: Default Values for Optionals
- Raw Identifiers in Swift 6.2
- How to use Debounce in SwiftUI or in Observable classes
- A Pull-Cord Theme Switcher in SwiftUI