LMTextView
Overview
The LMTextView
is a customizable text view widget that extends UITextView
with additional functionality for placeholder text, action restrictions, keyboard management, and text alignment. It provides convenient methods for managing text input behavior and user interactions.
File Location:
LMTextView.swift
Functionality
UI Components
text
: The text view's text contentplaceHolderText
: The placeholder text displayed when the text view is emptyinputAccessoryView
: The accessory view displayed above the keyboardcontentOffset
: The offset of the text view's content
Properties
canPerformActionRestriction
: Boolean flag to restrict text view actions (copy, paste, select, etc.)numberOfLines
: Computed property that returns the number of lines in the text viewplaceHolderText
: String property for placeholder text with automatic color management
Methods
translatesAutoresizingMaskIntoConstraints() -> Self
: Disables autoresizing mask translation and returns self for chainingcanPerformAction(_:withSender:)
: Overridden method to handle action restrictionsalignTextVerticallyInContainer()
: Centers text vertically within the text view containeraddDoneButtonOnKeyboard()
: Adds a "Done" button to the keyboard's accessory viewdoneButtonAction()
: Objective-C method that resigns the text view's first responder status
Customization
CustomLMTextView.swift
class CustomLMTextView: LMTextView {
override init(frame: CGRect, textContainer: NSTextContainer?) {
super.init(frame: frame, textContainer: textContainer)
setupDefaultAppearance()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
setupDefaultAppearance()
}
private func setupDefaultAppearance() {
font = .systemFont(ofSize: 16)
textColor = .label
backgroundColor = .systemBackground
layer.cornerRadius = 8
layer.borderWidth = 1
layer.borderColor = UIColor.systemGray4.cgColor
}
override func addDoneButtonOnKeyboard() {
super.addDoneButtonOnKeyboard()
// Add custom keyboard accessory behavior
}
override func doneButtonAction() {
super.doneButtonAction()
// Add custom done button behavior
}
}
AppDelegate.swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
LMUIComponents.shared.customTextView = CustomLMTextView.self
// ...
return true
}