9.3 C
Canberra
Tuesday, July 1, 2025

Create a multi-line, editable textual content view utilizing TextEditor in SwiftUI. – iOSTutorialJunction


In UIKit, we use UITextView for enter fields that require lengthy textual content entries from the consumer. In SwiftUI, we will accomplish the identical with TextEditor. The TextEditor view in SwiftUI handles multiline textual content enter. On this tutorial, we’ll learn to use TextEditor with a placeholder, making it perform equally to a UITextView in UIKit.

Step-by-Step Implementation of multi-line editable textual content discipline in SwiftUI

Step 1: Open Xcode and choose “Create a brand new Xcode undertaking.“. Select “App” underneath the iOS tab.Title your undertaking and ensure “SwiftUI” is chosen for the Person Interface.

Step 2: Create a state variable named multiLineText.

import SwiftUI

struct FeedbackView: View {
   @State non-public var multiLineText = ""
   var physique: some View {
   }

}

Step 3: Let begin designing our multi-line textual content discipline in SwiftUI utilizing TextEditor.

  • First we’ll take VStack with alignment to main as we wish our view to to not be in centre of display screen however towards the forefront.
  • Add a Textual content view with title “Remark” illustrating consumer the he can add feedback beneath
  • Take a ZStack with alignment to topLeading and inside it first we’ll add a RoundedRectangle view in order that we can provide border to our TextEditor. Then will test if state variable named multiLineText is empty or not. If it’s empty we’ll add placeholder textual content utilizing Textual content view.
  • Lastly we’ll add TextEditor to our ZStack. Lastly we’ll change opacity of TextEditor if it’s empty so as to let placeholder Textual content seen to consumer.

Full code snippet is given beneath.

import SwiftUI

struct ContentView: View {
    @State non-public var multiLineText = ""
    var physique: some View {
        VStack(alignment: .main) {
            Textual content("Remark")
                .font(.system(measurement: 14.0, weight: .medium))
            ZStack(alignment: .topLeading) {
                RoundedRectangle(cornerRadius: 14, fashion: .steady)
                    .strokeBorder(Shade.crimson.opacity(0.6), lineWidth: 1)
                if multiLineText.isEmpty {
                    Textual content("Placeholder Textual content")
                        .foregroundColor(Shade.grey)
                        .font(.system(measurement: 14.0, weight: .common))
                        .padding(.horizontal, 8)
                        .padding(.vertical, 12)
                }
                TextEditor(textual content: $multiLineText)
                    .font(.system(measurement: 14.0))
                    .opacity(multiLineText.isEmpty ? 0.7 : 1)
                    .padding(4)
                
            }
            .body(peak: 135)
        }
        .padding()
    }
}

#Preview {
    ContentView()
}

Output of utilizing TextEditor with Placeholder in SwiftUI

Create a multi-line, editable textual content view utilizing TextEditor in SwiftUI. – iOSTutorialJunction

This code snippets provides you with a multi-line editable textual content discipline in SwiftUI with a placeholder that disappears as soon as the consumer begins typing and re – seems if its empty



Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

[td_block_social_counter facebook="tagdiv" twitter="tagdivofficial" youtube="tagdiv" style="style8 td-social-boxed td-social-font-icons" tdc_css="eyJhbGwiOnsibWFyZ2luLWJvdHRvbSI6IjM4IiwiZGlzcGxheSI6IiJ9LCJwb3J0cmFpdCI6eyJtYXJnaW4tYm90dG9tIjoiMzAiLCJkaXNwbGF5IjoiIn0sInBvcnRyYWl0X21heF93aWR0aCI6MTAxOCwicG9ydHJhaXRfbWluX3dpZHRoIjo3Njh9" custom_title="Stay Connected" block_template_id="td_block_template_8" f_header_font_family="712" f_header_font_transform="uppercase" f_header_font_weight="500" f_header_font_size="17" border_color="#dd3333"]
- Advertisement -spot_img

Latest Articles