Skip to content

Custom TextField component SwiftUI. It is built upon programmatic UI and fully customizable.

License

Notifications You must be signed in to change notification settings

esatgozcu/SwiftUI-Custom-TextField

Repository files navigation

Logo

Swift 5.6 SwiftUI MIT License

SwiftUI Custom TextField

This repository developed with only swiftUI, there is no UIViewRepresentable.

Requirements

  • iOS 13.0+
  • Xcode 13+
  • Swift 5+

Swift Package Manager

CustomTextfield is available through Swift Package Manager. Add CustomTextField as a dependency to your Package.swift:

.package(url: "https://github.com/esatgozcu/SwiftUI-Custom-TextField", from: "main")

Customize config

You can set your config properties in @main so you don't need to set every time.

@main
struct ExampleApp: App {
    init() {
        let shared = EGTextFieldConfig.shared
        shared.defaultTextColor = .black
        shared.darkModeTextColor = .white
        shared.defaultTitleColor = .black
        shared.darkModeTitleColor = .white
        shared.titleFont = .body
        shared.borderType = .line
        ...
    }
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

Dark Mode

Dark mode ability is available. You don't need to consider this issue. If you want to change default color you can set in config file.

Default initialize

@State var text = "Esat Gozcu"

EGTextField(text: $text)
                .setTitleText("Title")

Light

Light Mode

Dark

Dark Mode

Examples

Default

@State var text = "Esat Gozcu"

EGTextField(text: $text)

Default

Line Border

@State var text = "Esat Gozcu"

EGTextField(text: $text)
                .setBorderType(.line)

Line Border

Title

@State var text = "Esat Gozcu"

EGTextField(text: $text)
                .setTitleText("Name")
                .setTitleColor(.blue)
                .setTitleFont(.body)

Title

PlaceHolder

@State var text = ""

EGTextField(text: $text)
                .setPlaceHolderText("Enter a name")

PlaceHolder

Also if you want you can change placeholder color

@State var text = ""

EGTextField(text: $text)
                .setPlaceHolderText("Enter a name")
                .setPlaceHolderTextColor(Color.gray)

Disable

@State var text = "Esat Gozcu"
@State var disable = true

EGTextField(text: $text)
                .setDisable($disable)
                .setDisableColor(Color.gray.opacity(0.2))

Disable

Bottom Error

@State var text = "Esat Gozcu"
@State var error = true
@State var errorText = "Your name is not matched"

EGTextField(text: $text)
                .setError(errorText: $errorText, error: $error)

Bottom Error

Trailing Image

@State var text = "Esat Gozcu"

EGTextField(text: $text)
                .setTrailingImage(Image(systemName: "qrcode"), click: {
                    print("qr image tapped")
                })

Trailing Image

You can also set image foreground color.

EGTextField(text: $text)
                .setTrailingImageForegroundColor(.black)
                .setDarkModeTrailingImageForegroundColor(.white)

⚠️ Warning: If you do not use system image. When you add your image, you have to set your 'Render as -> Template Image'. Otherwise you can not use this foreground color feature.

Secure Text

@State var text = "Esat Gozcu"

EGTextField(text: $text)
               .setSecureText(true)

Secure Text Close

Secure Text Open

Secure text images can be changed

@State var text = "Esat Gozcu"

EGTextField(text: $text)
                .setSecureText(true)
                .setSecureTextImages(open : Image(systemName: "eye.fill"),
                                     close: Image(systemName: "eye.slash.fill"))

Max Count

@State var text = "Esat Gozcu"

EGTextField(text: $text)
                .setMaxCount(8)

Max Count

Truncation Mode

@State var text = "esat1feyk23mqjyefycrfk862x78cyk2sy9l0t8rt6c"

EGTextField(text: $text)
                .setTruncateMode(.middle)

Truncation Mode

Border Color and Width

@State var text = "Esat Gozcu"

EGTextField(text: $text)
                .setBorderColor(.orange)
                .setBorderWidth(1.0)

Border Color

Text Color

@State var text = "Esat Gozcu"

EGTextField(text: $text)
                .setTextColor(.blue)

Text Color

Background Color

@State var text = "Esat Gozcu"

EGTextField(text: $text)
            .setBackgroundColor(Color.black)
            .setTextColor(Color.white)

Background Color

Corner Radius

@State var text = "Esat Gozcu"

EGTextField(text: $text)
            .setCornerRadius(20.0)

Corner Radius

Disable Auto Correction

@State var text = "Esat Gozcu"

EGTextField(text: $text)
             .setDisableAutoCorrection(true)

Set Height

@State var text = "Esat Gozcu"

EGTextField(text: $text)
                .setTextFieldHeight(45)

Focused Border Color

@State var text = "Esat Gozcu"

EGTextField(text: $text)
                .setFocusedBorderColorEnable(true)

Normal

Focused

If you want you can change colors.

@State var text = "Esat Gozcu"

EGTextField(text: $text)
                .setFocusedBorderColorEnable(true)
                .setFocusedBorderColor(.black)
                .setDarkModeFocusedBorderColor(.white)

⚠️ Warning: This feauture is not working on secureField.