SwiftyUIX is a powerful and versatile library for iOS and macOS developers that provides a collection of useful extensions and pre-defined views. With SwiftyUIX, you can enhance your development workflow, improve code readability, and accelerate app development by leveraging its handy utilities.
To integrate SwiftyUIX into your Xcode project using CocoaPods, add the following line to your Podfile:
pod 'SwiftyUIX'
Then, run the following command:
$ pod install
You can also use Swift Package Manager to add SwiftyUIX to your project. In Xcode, go to File -> Swift Packages -> Add Package Dependency... and enter the URL of this repository:
https://github.com/Mokshsuthar/SwiftyUIX.git
Follow the prompts to complete the installation.
BlurView
The BlurView
is a SwiftUI view that creates a visual blur effect, primarily designed for iOS.
- Visual Blur Effect:
BlurView
is aUIViewRepresentable
that wraps aUIVisualEffectView
to apply a blur effect to its content based on the specifiedUIBlurEffect.Style
. - Customizable Style: You can customize the blur effect by providing a different
UIBlurEffect.Style
during initialization. The default style is.systemChromeMaterial
, representing the standard system blur appearance.
import SwiftUI
struct ContentView: View {
var body: some View {
ZStack {
// Your content here...
// Apply a blur effect to the content
BlurView(style: .systemUltraThinMaterial)
}
}
}
- iOS 13.0 or later
HTMLWebView
The HTMLWebView
is a SwiftUI view that displays a WKWebView
with HTML content from a specified file path.
- Load HTML Content:
HTMLWebView
is aUIViewRepresentable
that loads and displays HTML content from a given file path in aWKWebView
. - Transparent Background: The
WKWebView
and its scroll view have a transparent background, allowing seamless integration with other SwiftUI views. - Optional Scroll Event Handling: You can optionally receive scroll events from the web view's scroll view through the
scrollViewDidScroll
closure parameter.
import SwiftUI
struct ContentView: View {
var body: some View {
VStack {
// Your other SwiftUI views here...
// Display the HTML content from a specified file path
HTMLWebView(filePath: Bundle.main.path(forResource: "sample", ofType: "html"))
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
}
}
}
- iOS 13.0 or later for
HTMLWebView
on iOS. - macOS 11.0 or later for
HTMLWebView
on macOS.
TransparentBackground
The TransparentBackground
is a SwiftUI UIViewRepresentable
that makes the background transparent for a presented sheet or fullscreen cover, primarily designed for iOS.
Setting the background color of the superview's superview directly (as done in this struct) might have unintended side effects and may not be reliable across all SwiftUI versions or implementations. It's essential to thoroughly test this code and consider potential edge cases before using it in production.
import SwiftUI
struct ContentView: View {
@State private var isSheetPresented = false
var body: some View {
Button("Present Sheet") {
isSheetPresented.toggle()
}
.sheet(isPresented: $isSheetPresented, content: {
// Your sheet content here...
YourView()
.background(TransparentBackground()) // Apply the transparent background to the presented sheet
})
}
}
- iOS 13.0 or later
swiftui Views Extensions
-
Device Dimensions
screenWidth
: Retrieves the width of the device screen inCGFloat
.screenHeight
: Retrieves the height of the device screen inCGFloat
.topSafeAreaHeight
: Retrieves the height of the top safe area (notch, if applicable) inCGFloat
.bottomSafeAreaHeight
: Retrieves the height of the bottom safe area inCGFloat
.
-
Screen Corner Radius
screenCornerRadius(minimum:)
: Calculates and returns the corner radius of the device's display. Optional minimum value can be set.
-
Home Indicator
hideHomeIndicator()
: Hides the home indicator on devices running iOS 16.0 or later. Compatibility handled for earlier iOS versions.
-
Haptic Feedback
playHapticFeedback(_:)
: Plays haptic feedback using the specifiedfeedbackType
.
-
View Frame Manipulation
fullframe(alignment:)
: Makes a view occupy the full parent's space with optional alignment.fullWidth(height:alignment:)
: Sets the frame with full available width and optional height.fullHeight(width:alignment:)
: Sets the frame with full available height and optional width.squareFrame(size:alignment:)
: Creates a square frame for views with a specified size and alignment.
-
Safe Area Ignoring
ignoreSafeArea_C()
: Ignores the safe area insets of the device on iOS 14 and above.
-
View Masking
cornerRadius(_:)
: Adds a continuous corner radius to the view.
-
Scroll Position Detection
getScrollPosition(key:handler:)
: Detects the scroll position of a view and calls the handler with the offset.
-
System Icon Image View
systemImage(_:)
: Creates an image view with a system icon.
- View Border
border(lineWidth:cornerRadius:color:)
: Adds a border to the view with specified line width, corner radius, and color.
SwiftUI Image Extensions
A set of helpful SwiftUI extensions to manipulate image views easily.
-
Square Frame with Aspect Ratio
squareFrameWithApectRatio(value:contentMode:)
: Resizes the image to a square frame with a specified size while maintaining its aspect ratio. ThecontentMode
parameter allows you to control how the image fills the frame.
-
Resize with Aspect Ratio
resizeWithApectRatio(contentMode:)
: Resizes the image while maintaining its aspect ratio. ThecontentMode
parameter lets you control how the image fits within its new frame.
import SwiftUI
struct ContentView: View {
var body: some View {
Image("exampleImage")
.squareFrameWithApectRatio(value: 100, contentMode: .fill)
.border(lineWidth: 2, cornerRadius: 10, color: .blue)
}
}
Thread Extensions
A collection of useful extensions for managing threads in Swift.
-
On Main Thread Execution
OnMainThread(_:)
: Executes the given closure on the main thread if the current thread is already the main thread. Otherwise, dispatches it asynchronously to the main thread for execution.
-
On Background Thread Execution
OnBackGroudThread(_:)
: Executes the given closure on a background thread using a global background queue.
-
Delayed Execution on Main Thread
runAfter(_:completion:)
: Schedules the given closure to run after a specified delay (in seconds) on the main thread usingDispatchQueue.main.asyncAfter
.
-
Custom Thread Creation
startNewThread(name:qos:execute:)
: Creates and manages custom threads with specific configurations. This extension allows you to start a new thread with a custom name and Quality of Service (QoS).
import Foundation
// Execute a closure on the main thread after a delay of 2 seconds
Thread.runAfter(2) {
print("This will be executed after 2 seconds on the main thread.")
}
// Execute a closure on the main thread
Thread.OnMainThread {
print("This will be executed on the main thread.")
}
// Execute a closure on a background thread
Thread.OnBackGroudThread {
print("This will be executed on a background thread.")
}
// Create and start a custom thread
Thread.startNewThread(name: "CustomThread", qos: .userInitiated) {
print("This is a custom thread with name 'CustomThread' and QoS 'userInitiated'.")
}
Date Extensions
A set of useful extensions for working with Date objects in Swift.
-
Readable Time
getReadableTime()
: Formats a Date object into a human-readable time string in a 12-hour format with AM/PM indicator. Example output: "03:30 PM" or "11:45 AM".
-
Readable Date
getReadableDate()
: Formats a Date object into a human-readable date string in the format "MM/DD/YYYY". Example output: "07/22/2023" or "01/05/2024".
-
Readable Date and Time
getReadableDateTime()
: Formats a Date object into a human-readable date and time string. Example output: "07/22/2023 03:30 PM" or "01/05/2024 11:45 AM".
-
Timestamp Generation
TimeStemp(format:)
: Provides a unique timestamp according to the given format. The default format is "yyyyMMdd'T'HHmmssSSS".
-
Month and Day Names
getMonthName()
: Gets the full month name from the date, like "January," "February," etc.getShortMonthName()
: Gets the short month name from the date, like "Jan," "Feb," etc.getDayName()
: Gets the full day name from the date, like "Sunday," "Monday," etc.getShortDayName()
: Gets the short day name from the date, like "Sun," "Mon," etc.
-
Date Component Retrieval
getDateComponent(_:calendar:)
: Returns the value of a specific component (e.g., day, month, year, hour, minute) from the Date object using the provided Calendar. If no Calendar is provided, it defaults to the current calendar.
import Foundation
let currentDate = Date()
let readableTime = currentDate.getReadableTime()
print("Readable Time: \(readableTime)")
let readableDate = currentDate.getReadableDate()
print("Readable Date: \(readableDate)")
let readableDateTime = currentDate.getReadableDateTime()
print("Readable Date and Time: \(readableDateTime)")
let timestamp = currentDate.TimeStemp()
print("Timestamp: \(timestamp)")
let monthName = currentDate.getMonthName()
print("Month Name: \(monthName)")
let shortMonthName = currentDate.getShortMonthName()
print("Short Month Name: \(shortMonthName)")
let dayName = currentDate.getDayName()
print("Day Name: \(dayName)")
let shortDayName = currentDate.getShortDayName()
print("Short Day Name: \(shortDayName)")
let day = currentDate.getDateComponent(.day)
print("Day: \(day)")
let month = currentDate.getDateComponent(.month)
print("Month: \(month)")
let year = currentDate.getDateComponent(.year)
print("Year: \(year)")
Data Extension
A convenient extension for working with Data objects in Swift.
- Readable Data Size
getReadableDataSize()
: Converts the size of a Data object into a human-readable format, displaying units like KB, MB, GB, etc.
import Foundation
let testData = Data(repeating: 0, count: 1024) // 1 KB
let readableSize = testData.getReadableDataSize()
print("Readable Size: \(readableSize)") // Output: "1 KB"
Color Extensions
-
Initialization from Hex String
init(hexString:)
: Initializes a Color object from a valid hex color string (e.g., "#RRGGBB" or "#AARRGGBB").
-
Conversion to Hex String
toHex()
: Converts the Color object to its hex representation. For macOS, this function is only available on macOS 11.0 and later, and for iOS, it requires iOS 14.0 and later.
-
Conversion from
UIColor
(iOS) orNSColor
(macOS) to Hex CodetoHexCode()
: Converts aUIColor
object (iOS) orNSColor
object (macOS) to its hex representation.
import SwiftUI
let redColor = Color(hexString: "#FF0000")
let hexCode = redColor.toHex()
print("Hex Code: \(hexCode ?? "Unknown")") // Output: "Hex Code: #FF0000"
#if os(iOS)
if let uiColor = UIColor(red: 0.5, green: 0.5, blue: 0.5, alpha: 1.0).toHexCode() {
print("UI Color Hex Code: \(uiColor)") // Output: "UI Color Hex Code: #7F7F7F"
}
#elseif os(macOS)
if let nsColor = NSColor(red: 0.5, green: 0.5, blue: 0.5, alpha: 1.0).toHexCode() {
print("NS Color Hex Code: \(nsColor)") // Output: "NS Color Hex Code: #7F7F7F"
}
#endif
This project is licensed under the MIT License. Feel free to use and modify it as per your requirements.