TTBaseUITextView
CoreMulti-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)
}
| Type | Border | Use Case |
|---|---|---|
.DEFAULT | 1pt rounded border | Standard multi-line text fields |
.NO_BORDER | None | Custom 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 / Property | Returns | Description |
|---|---|---|
onTextEditChangedHandler | Closure? | Called on every text change |
onDismissKeyboard | Closure? | Called when accessory dismiss button tapped |
onDidMaxLeightHandle | Closure? | Called when max length is exceeded |
onTouchReturnKeyHandler | Closure? | Called on Return key press |
editingDidBeginHandle | Closure? | Called when editing starts |
editingDidEndHandle | Closure? | Called when editing ends |
maxNumberOfLines | Int | Max lines before truncation (override in subclass, default 4) |
setText(text:) | Void | Set text content programmatically |
setTextColor(color:) | TTBaseUITextView | Change text color (chainable) |
setBgColor(_:) | Void | Set background color |
setNoBorder() | TTBaseUITextView | Remove all borders (chainable) |
setMaxLenght(max:) | Void | Set maximum character count |
setAutoSizeWhenInputText(withMaximumNumberOfLines:) | Void | Enable auto-height growing up to N lines |
setHiddenKeyboardAccessoryView() | TTBaseUITextView | Add dismiss keyboard accessory bar |
setKeyboardStyleBy*() | TTBaseUITextView | Email/Number/Phone/Money/Date keyboard |
setPasswordView() | Void | Enable 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.