TTBaseUITextView

Core

Multi-line UITextView subclass with 2 border modes, configurable padding, auto-dismiss keyboard accessory, real-time text change handler, max-length enforcement, auto-size to fit content, character limit callback, and chainable API.


🎨 Field Types

public enum TYPE {
    case DEFAULT    // 1pt border with corner radius
    case NO_BORDER  // No border (embed in styled container)
}
TypeBorderUse Case
.DEFAULT1pt rounded borderStandard multi-line text fields
.NO_BORDERNoneCustom styled panels/containers

🚀 Usage

Basic Multi-line Input

// Default bordered text view
let commentView = TTBaseUITextView(with: .DEFAULT)
commentView.setHeightAnchor(constant: 120)

// No border variant
let noteView = TTBaseUITextView(with: .NO_BORDER)

// Without keyboard dismiss accessory
let bioView = TTBaseUITextView(
    with: .DEFAULT,
    isSetHiddenKeyboardAccessoryView: false
)

Real-time Change Handler

let descriptionView = TTBaseUITextView()
descriptionView.onTextEditChangedHandler = { view, text in
    charCountLabel.text = "\(text.count)/200"
    submitBtn.setEnable()
}

// Editing began / ended
descriptionView.editingDidBeginHandle = { view in
    view.layer.borderColor = UIColor.systemBlue.cgColor
}
descriptionView.editingDidEndHandle = { view in
    view.layer.borderColor = UIColor.lightGray.cgColor
}

Max Length Enforcement

let bioView = TTBaseUITextView()
bioView.setMaxLenght(max: 200)  // Block input over 200 chars

// Callback when limit is reached
bioView.onDidMaxLeightHandle = { text in
    showToast("Max 200 characters allowed")
}

Auto-sizing (Grows with Content)

// Grows up to 5 lines, then scrolls
let messageView = TTBaseUITextView()
messageView.setAutoSizeWhenInputText(withMaximumNumberOfLines: 5)

Return Key Handling

let chatInput = TTBaseUITextView()
chatInput.returnKeyType = .send
chatInput.onTouchReturnKeyHandler = { view, keyType in
    if keyType == .send {
        sendMessage(view.text)
        view.text = ""
    }
}

📖 API Reference

Method / PropertyReturnsDescription
onTextEditChangedHandlerClosure?Called on every text change
onDismissKeyboardClosure?Called when accessory dismiss button tapped
onDidMaxLeightHandleClosure?Called when max length is exceeded
onTouchReturnKeyHandlerClosure?Called on Return key press
editingDidBeginHandleClosure?Called when editing starts
editingDidEndHandleClosure?Called when editing ends
maxNumberOfLinesIntMax lines before truncation (override in subclass, default 4)
setText(text:)VoidSet text content programmatically
setTextColor(color:)TTBaseUITextViewChange text color (chainable)
setBgColor(_:)VoidSet background color
setNoBorder()TTBaseUITextViewRemove all borders (chainable)
setMaxLenght(max:)VoidSet maximum character count
setAutoSizeWhenInputText(withMaximumNumberOfLines:)VoidEnable auto-height growing up to N lines
setHiddenKeyboardAccessoryView()TTBaseUITextViewAdd dismiss keyboard accessory bar
setKeyboardStyleBy*()TTBaseUITextViewEmail/Number/Phone/Money/Date keyboard
setPasswordView()VoidEnable secure text entry

⚙️ Configuration

// Subclass to customize padding and line count
class MyTextView: TTBaseUITextView {
    override var maxNumberOfLines: Int { return 6 }
    override var paddingTextUIEdgeInsets: UIEdgeInsets {
        return UIEdgeInsets(top: 12, left: 12, bottom: 12, right: 12)
    }
    override func updateUI() {
        self.setBgColor(.systemGray6)
    }
}
Pro Tip: Override maxNumberOfLines and paddingTextUIEdgeInsets in a subclass to quickly get consistent multi-line inputs across your app without repeating setup code.