Skip to main content

LMButton

Overview

The LMButton is a customizable button widget that extends UIButton with additional functionality for styling, layout, and state management. It provides convenient methods for configuring appearance and managing content layout with icons and text.

File Location:
LMButton.swift

Functionality

UI Components

  • titleLabel: The button's title label for text display
  • imageView: The button's image view for icon display
  • layer: The button's layer for shadow and corner radius customization

Methods

  • init(frame:): Initializes the button with a frame
  • createButton(with:image:textColor:textFont:contentSpacing:imageSpacing:): Factory method to create a pre-configured button instance
  • translatesAutoresizingMaskIntoConstraints() -> Self: Disables autoresizing mask translation and returns self for chaining
  • setContentInsets(with:): Sets content insets for iOS 15+ compatibility
  • setInsets(forContentPadding:imageTitlePadding:): Sets content and title edge insets for icon-text spacing
  • setImageInsets(with:): Sets image edge insets for iOS 15+ compatibility
  • centerTextAndImage(spacing:): Centers text and image with specified spacing, supporting RTL layouts
  • setFont(_:): Sets the button's title label font
  • addShadow(): Adds a default shadow effect to the button

Customization

CustomLMButton.swift
class CustomLMButton: LMButton {
override init(frame: CGRect) {
super.init(frame: frame)
setupDefaultAppearance()
}

private func setupDefaultAppearance() {
backgroundColor = .systemBlue
setTitleColor(.white, for: .normal)
layer.cornerRadius = 12
addShadow()
}

override func setFont(_ font: UIFont) {
super.setFont(font)
// Add custom font configuration
}

override func addShadow() {
super.addShadow()
// Customize shadow properties
layer.shadowColor = UIColor.systemBlue.cgColor
layer.shadowOpacity = 0.4
}
}
AppDelegate.swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
LMUIComponents.shared.customButton = CustomLMButton.self
// ...
return true
}