Skip to main content

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 content
  • placeHolderText: The placeholder text displayed when the text view is empty
  • inputAccessoryView: The accessory view displayed above the keyboard
  • contentOffset: 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 view
  • placeHolderText: String property for placeholder text with automatic color management

Methods

  • translatesAutoresizingMaskIntoConstraints() -> Self: Disables autoresizing mask translation and returns self for chaining
  • canPerformAction(_:withSender:): Overridden method to handle action restrictions
  • alignTextVerticallyInContainer(): Centers text vertically within the text view container
  • addDoneButtonOnKeyboard(): Adds a "Done" button to the keyboard's accessory view
  • doneButtonAction(): 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
}