Skip to main content

LMLabel

Overview

The LMLabel is a customizable label widget that extends UILabel with additional functionality for text padding and layout management. It provides convenient methods for configuring text insets and managing content layout with customizable padding options.

File Location:
LMLabel.swift

Functionality

UI Components

  • textEdgeInsets: The edge insets applied to the text content
  • text: The label's text content
  • layer: The label's layer for styling customization

Methods

  • init(frame:): Initializes the label with a frame
  • translatesAutoresizingMaskIntoConstraints() -> Self: Disables autoresizing mask translation and returns self for chaining
  • textRect(forBounds:limitedToNumberOfLines:): Overridden method to handle text insets in text rect calculation
  • drawText(in:): Overridden method to apply text insets during drawing
  • setPadding(with:): Sets padding for all edges using UIEdgeInsets
  • paddingLeft: IBInspectable property for left padding
  • paddingRight: IBInspectable property for right padding
  • paddingTop: IBInspectable property for top padding
  • paddingBottom: IBInspectable property for bottom padding

Customization

CustomLMLabel.swift
class CustomLMLabel: LMLabel {
override init(frame: CGRect) {
super.init(frame: frame)
setupDefaultAppearance()
}

required init?(coder: NSCoder) {
super.init(coder: coder)
setupDefaultAppearance()
}

private func setupDefaultAppearance() {
textColor = .label
font = .systemFont(ofSize: 16)
numberOfLines = 0
lineBreakMode = .byWordWrapping
}

override func setPadding(with padding: UIEdgeInsets) {
super.setPadding(with: padding)
// Add custom padding behavior
}
}
AppDelegate.swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
LMUIComponents.shared.customLabel = CustomLMLabel.self
// ...
return true
}