ElementBuilder is a Swift framework designed to simplify the creation and management of user interface elements in UIKit-based iOS applications. It provides factory structs for reusable, customizable UI elements with minimal code. Additionally, it includes tools for handling swipe gestures and working with dates.
- 🧩 Modular UI Builders: buttons, datePicker, gradient, image, label, stackView, textField, uiView.
- 🌈 Gradient Layers: Easily add gradient backgrounds to your views.
- 🎛️ Swipe Gesture Manager: Simplify swipe gesture handling in your view controllers.
- 📆 Date Utilities: Format and display dates in your UI seamlessly.
- ButtonBuilder
- DateAndWeekDayFormatter
- DatePickerBuilder
- GradientBuilder
- ImageViewBuilder
- LabelBuilder
- StackViewBuilder
- SwipeGestureManager
- TextFieldBuilder
- UIViewBuilder
- iOS 17+
- Xcode 15+
- Swift 5.9
To add the ElementBuilder library to your Xcode project, follow these steps:
1.Open the "Package Dependencies" section in Xcode: select the “Package Dependencies” tab in the project settings.
2.Add a dependency: click the '+' button to add a new package.
3.Enter the repository URL: Paste the repository URL and click “Add Package.”.
4.Import the Library: In the files where you want to use ElementBuilder, import it by adding:
import ElementBuilder
- Example: Creating a Login Screen
- Here’s how to build a basic login screen using ElementBuilder:
import UIKit
import SnapKit
import ElementBuilder
class LoginViewController: UIViewController {
let titleLabel = LabelBuilder.label(
TextInputSetting(
text: "Title Label",
textColor: .black,
textAlignment: .center,
fontSize: 30,
weight: .bold
),
LabelSetting(
backgroundColor: .clear,
clipsToBounds: false
)
)
let buttonCloseView = ButtonBuilder.buttonSystemAndButtonImage(
ButtonAppearanceAttributes(
backgroundColor: .clear,
cornerRadius: 17,
clipsToBounds: false
),
ButtonImageAttributes(
systemName: "x.circle",
tintColor: .red,
alpha: 1,
imageSize: CGSize(width: 39, height: 38)
),
ShadowSetting(
shadowColor: UIColor.systemRed.cgColor,
shadowOffset: CGSize(width: 0, height: 0),
shadowOpacity: 0.6,
shadowRadius: 3
)
)
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(titleLabel)
view.addSubview(buttonCloseView)
titleLabel.snp.makeConstraints { make in
make.top.equalTo(10)
make.leading.equalTo(10)
make.trailing.equalTo(-10)
}
buttonCloseView.snp.makeConstraints { make in
make.top.equalTo(10)
make.leading.equalTo(20)
}
}
}
Here’s what each factory does and how it simplifies UI creation:
- Purpose: Creates customizable buttons.
- Usage:
let buttonCloseView = ButtonBuilder.buttonSystemAndButtonImage(
ButtonAppearanceAttributes(
backgroundColor: .clear,
cornerRadius: 17,
clipsToBounds: false
),
ButtonImageAttributes(
systemName: "x.circle",
tintColor: .red,
alpha: 1,
imageSize: CGSize(width: 39, height: 38)
),
ShadowSetting(
shadowColor: UIColor.systemRed.cgColor,
shadowOffset: CGSize(width: 0, height: 0),
shadowOpacity: 0.6,
shadowRadius: 3
)
)
- Purpose: Simplifies date formatting and displays dates with corresponding weekdays in UI elements like stack views.
- Usage:
import UIKit
import ElementBuilder
class MyViewController: UIViewController {
let stackView = StackViewBuilder.stackView(
StackViewSetting(
axis: .horizontal,
spacing: 10,
layoutMargins: .init(top: 10, left: 10, bottom: 10, right: 10),
distribution: .fillEqually,
backgroundColor: .clear,
cornerRadius: 10
)
)
let dateFormatter = DateAndWeekDayFormatter()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(stackView)
dateCalendar()
}
func dateCalendar() {
let calendar = Calendar.current
for i in 0..<3 {
let date = calendar.date(byAdding: .day, value: i, to: Date())!
dateFormatter.dateCurrent(to: stackView, withDay: date)
}
}
}
- Purpose: Creates a date picker with customizable styles.
- Usage:
let datePicker = DatePickerBuilder.datePicker(
DatePickerSetting(
datePickerMode: .time,
preferredDatePickerStyle: .wheels,
backgroundColor: .systemGray6,
cornerRadius: 15,
masksToBounds: true
)
)
- Purpose: Adds gradient layers to views.
- Usage:
let gradientController = GradientBuilder.gradient(
firstColor: .systemBlue,
secondColor: .systemIndigo
)
let gradientTableView = GradientBuilder.gradient(
firstColor: .systemBlue,
secondColor: .systemIndigo,
alphaColor: 0.6
)
- Purpose: Creates image views with system or custom images, supporting various styles.
- Usage:
let yourImage = ImageViewBuilder.imageAndSystemImage(
named: "yourNameImage",
cornerRadius: 20,
contentMode: . scaleAspectFit,
clipsToBounds: true
)
let systemImage = ImageViewBuilder.imageAndSystemImage(
systemName: "plus.circle",
tintColor: .red,
contentMode: . scaleAspectFit
)
- Purpose: Builds labels with specific text and style attributes.
- Usage:
let titleLabel = LabelBuilder.label(
TextInputSetting(
text: "Title Label",
textColor: .black,
textAlignment: .center,
fontSize: 30,
weight: .bold
),
LabelSetting(
backgroundColor: .clear,
clipsToBounds: false
)
)
- Purpose: Creates stack views with flexible layouts.
- Usage:
let stackView = StackViewBuilder.stackView(
StackViewSetting(
axis: .horizontal,
spacing: 10,
layoutMargins: .init(top: 10, left: 10, bottom: 10, right: 10),
distribution: .fillEqually,
backgroundColor: .clear,
cornerRadius: 10
)
)
- Purpose: Simplifies swipe gesture handling.
- Usage:
import UIKit
import ElementBuilder
class MyViewController: UIViewController {
private var swipeManager: SwipeGestureManager?
override func viewDidLoad() {
super.viewDidLoad()
swipe()
}
func swipe() {
swipeManager = SwipeGestureManager(
viewController: self,
leftAction: { },
rightAction: { [weak self] in
self?.dismiss(animated: true, completion: nil)
}
)
}
}
- Purpose: Creates text fields with customizable appearance and shadow styles.
- Usage:
let textField = TextFieldBuilder.textField(
TextInputConfiguration(
placeholder: "text",
placeholderTextColor: .label,
textContentType: .name,
returnKeyType: .default
),
TextFieldAppearance(
backgroundColor: .systemGray6,
clipsToBounds: false
),
ShadowSetting(
shadowColor: UIColor.darkGray.cgColor,
shadowOffset: .init(width: 0, height: 0),
shadowOpacity: 1,
shadowRadius: 3
)
)
- Purpose: Generates views with customizable shadows and rounded corners.
- Usage:
let container = ContainerViewFactory.makeContainerView(
appearanceAttributes: ViewAppearanceAttributes(
backgroundColor: .systemGray6,
cornerRadius: 25
),
shadowAttributes: ShadowAttributes(
shadowColor: UIColor.darkGray.cgColor,
shadowOffset: .init(width: 0, height: 0),
shadowOpacity: 1,
shadowRadius: 3
)
)
ElementBuilder a framework offers developers powerful and convenient tools for creating and managing UI elements in UIKit-based applications. It reduces the amount of code needed for UI development and makes the process of building interfaces more intuitive and flexible.