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 displayimageView
: The button's image view for icon displaylayer
: The button's layer for shadow and corner radius customization
Methods
init(frame:)
: Initializes the button with a framecreateButton(with:image:textColor:textFont:contentSpacing:imageSpacing:)
: Factory method to create a pre-configured button instancetranslatesAutoresizingMaskIntoConstraints() -> Self
: Disables autoresizing mask translation and returns self for chainingsetContentInsets(with:)
: Sets content insets for iOS 15+ compatibilitysetInsets(forContentPadding:imageTitlePadding:)
: Sets content and title edge insets for icon-text spacingsetImageInsets(with:)
: Sets image edge insets for iOS 15+ compatibilitycenterTextAndImage(spacing:)
: Centers text and image with specified spacing, supporting RTL layoutssetFont(_:)
: Sets the button's title label fontaddShadow()
: 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
}