Skip to content

winwx/DynamicTextEditor

Repository files navigation

DynamicTextEditor

DynamicTextEditor is a SwiftUI-based TextEditor component that automatically adjusts its height according to the user’s input.

Similar to the chat input field in KakaoTalk, where the text field dynamically grows with the number of lines.


Language

🇰🇷 한국어 README | 🇺🇸 English README | 🇯🇵 日本語 README

✨ Features

  • ✅ Set maximum line count
  • ✅ Support for placeholder text
  • ✅ Automatically resizing TextEditor
  • ✅ Custom font support

🛠 Requirements

  • iOS 15+
  • SwiftUI

📦 Installation

Swift Package Manager (SPM)

dependencies: [
    .package(url: "https://github.com/winwx/DynamicTextEditor.git", branch: "main")
]

🚀 Usage

Basic Example

import SwiftUI
import DynamicTextEditor

@State var text: String = ""

var body: some View {
    DynamicTextEditor("Enter your text...", text: $text)
}

KakaoTalk Clone Example

import SwiftUI
import DynamicTextEditor

@State var text: String = ""

var body: some View {
    ...
    HStack(alignment: .bottom, spacing: 16) {
        DynamicTextEditor(
            "Type a message",
            text: $text
        )
        .setFont(uiFont: .systemFont(ofSize: 16))
        .setMaxLineCount(8)
        .setTextColor(.black)
        .setPlaceholderColor(.gray)
        .frame(minHeight: 24)

        emojiButton()
    }
    .padding(.vertical, 6)
    .padding(.horizontal, 8)
    .background(Color.textField_BG)
    .cornerRadius(20)
    ...
}


🎛 Modifier API

DynamicTextEditor provides a SwiftUI-style modifier API to customize its behavior and appearance.

🔤 setFont(uiFont:)

Sets the font using UIFont.

DynamicTextEditor("Input", text: $text)
    .setFont(uiFont: .systemFont(ofSize: 16, weight: .medium))

📏 setMaxLineCount(_:)

Sets the maximum number of lines. Default is 5.

DynamicTextEditor("Input", text: $text)
    .setMaxLineCount(3)

🎨 setTextColor(_:)

Sets the text color. Default is .black.

DynamicTextEditor("Input", text: $text)
    .setTextColor(.gray)

👤 setPlaceholderColor(_:)

Sets the placeholder color. Default is .gray.

DynamicTextEditor("Input", text: $text)
    .setPlaceholderColor(.black)

🧪 Custom Example

DynamicTextEditor("Write a comment...", text: $text)
    .setFont(uiFont: .systemFont(ofSize: 14))
    .setMaxLineCount(4)
    .setTextColor(.blue)
    .setPlaceholderColor(.red)

About

SwiftUI Dynamic Height Textfield Library

Topics

Resources

Stars

Watchers

Forks

Languages