Skip to content

Commit

Permalink
Update version
Browse files Browse the repository at this point in the history
  • Loading branch information
dinhquan committed Jan 8, 2022
1 parent 633015f commit 46b642e
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 121 deletions.
86 changes: 39 additions & 47 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ SwiftAlertView

A powerful customizable Alert View library written in Swift.

Feeling painful when working with `UIAlertController` or `SwiftUI alert`, `SwiftAlertView` is the best alternative for UIKit's UIAlertView and UIAlertController.
`SwiftAlertView` is the best alternative for `UIAlertController` and `SwiftUI alert`.
With `SwiftAlertView`, you can easily make your desired Alert View in some lines of code.

![](https://raw.githubusercontent.com/dinhquan/SwiftAlertView/master/SwiftAlertView/Images/demo.png)
Expand All @@ -15,15 +15,15 @@ With `SwiftAlertView`, you can easily make your desired Alert View in some lines
[CocoaPods](https://cocoapods.org) is a dependency manager for Cocoa projects. To integrate SwiftAlertView into your Xcode project using CocoaPods, specify it in your `Podfile`:

```ruby
pod 'SwiftAlertView', '~> 2.1.0'
pod 'SwiftAlertView', '~> 2.2.0'
```

#### Carthage

[Carthage](https://github.com/Carthage/Carthage) is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks. To integrate SwiftAlertView into your Xcode project using Carthage, specify it in your `Cartfile`:

```ogdl
github "https://github.com/dinhquan/SwiftAlertView" ~> 2.1.0
github "https://github.com/dinhquan/SwiftAlertView" ~> 2.2.0
```

#### Swift Package Manager
Expand All @@ -34,7 +34,7 @@ Once you have your Swift package set up, adding SwiftAlertView as a dependency i

```swift
dependencies: [
.package(url: "https://github.com/dinhquan/SwiftAlertView", .upToNextMajor(from: "2.1.0"))
.package(url: "https://github.com/dinhquan/SwiftAlertView", .upToNextMajor(from: "2.2.0"))
]
```

Expand All @@ -43,25 +43,22 @@ Drag and drop the file named ```SwiftAlertView``` inside `Source` in your projec

## Highlight Features

- Change the alert appearance: background color or background image, border radius.
- Change the title appearance: font, color, margin, spacing, visibility.
- Change the message appearance: font, color, margin, spacing, visibility.
- Change the button appearance: font, color.
- Change the separator appearance: color, visibility.
- Change the alert appear behaviour and disappear behaviour.
- Add text fields
- Change the alert background color, background image, border radius.
- Change the title font, color, margin, visibility.
- Change the message font, color, margin, visibility.
- Change the button font, color.
- Change the alert appearance behaviour.
- Add text fields (like UIAlertController)
- Support dark mode
- Initialize the alert view with a custom view.
- Initialize the alert view with a xib file.
- Closures and callbacks for handling button touched events.
- Callbacks for handling button touched events.
- And many more ...

## Usage

### Showing an alert

Showing an alert is easy as pie

```swift
SwiftAlertView.show(title: "Sample title", message: "Sample message", buttonTitles: "Cancel", "OK")
```
Expand All @@ -84,7 +81,7 @@ Handle button clicked events
SwiftAlertView.show(title: "Sample title",
message: "Sample message",
buttonTitles: "OK", "Cancel") {
alertView.style = .dark
$0.style = .dark
}
.onButtonClicked { _, buttonIndex in
print("Button Clicked At Index \(buttonIndex)")
Expand All @@ -94,15 +91,15 @@ SwiftAlertView.show(title: "Sample title",
Add text fields

```swift
SwiftAlertView.show(title: "Sign in", buttonTitles: "Cancel", "Sign In") { alertView in
alertView.addTextField { textField in
SwiftAlertView.show(title: "Sign in", buttonTitles: "Cancel", "Sign In") { alert in
alert.addTextField { textField in
textField.placeholder = "Username"
}
alertView.addTextField { textField in
alert.addTextField { textField in
textField.placeholder = "Password"
}
}
.onButtonClicked { alert, buttonIndex in
.onActionButtonClicked { alert, buttonIndex in
let username = alert.textField(at: 0)?.text ?? ""
print("Username: ", username)
}
Expand Down Expand Up @@ -141,7 +138,6 @@ alertView.show(in: view)

// Programmatically dismiss the alert view
alertView.dismiss()

```

Handle button clicked event
Expand All @@ -157,7 +153,6 @@ alertView.onCancelButtonClicked = {
alertView.onActionButtonClicked = { buttonIndex in
print("Action Button Clicked At Index \(buttonIndex)")
}

```

If you don't want to use closures, make your view controller conform ```SwiftAlertViewDelegate``` and use delegate methods:
Expand All @@ -176,56 +171,53 @@ func didPresentAlertView(_ alertView: SwiftAlertView) {
func didDismissAlertView(_ alertView: SwiftAlertView) {
println("Did Dismiss Alert View")
}

```
### Customization

SwiftAlertView can be customized with the following properties:

```swift
public var style: Style = .auto // default is base on system color
public var style: Style = .auto // default is based on system color

public var titleLabel: UILabel! // access titleLabel to customize the title font, color
public var messageLabel: UILabel! // access messageLabel to customize the message font, color

public var cancelButtonIndex = 0 // default is 0, set this property if you want to change the position of cancel button

public var backgroundImage: UIImage?
// public var backgroundColor: UIColor? // inherits from UIView

public var cancelButtonIndex = 0 // default is 0, set this property if you want to change the position of cancel button
public var buttonTitleColor = UIColor(red: 0, green: 0.478431, blue: 1, alpha: 1) // to change the title color of all buttons
public var buttonHeight: CGFloat = 44.0 // default is 44
public var buttonHeight: CGFloat = 44.0

public var separatorColor = UIColor(red: 196.0/255, green: 196.0/255, blue: 201.0/255, alpha: 1.0) // to change the separator color
public var isHideSeparator = false // to hide the separater color
public var cornerRadius: CGFloat = 12.0 // default is 8 px
public var isHideSeparator = false
public var cornerRadius: CGFloat = 12.0

public var isDismissOnActionButtonClicked = true // default is true, if you want the alert view will not be dismissed when clicking on action buttons, set this property to false
public var isHighlightOnButtonClicked = true // default is true
public var isDimBackgroundWhenShowing = true // default is true
public var isDismissOnOutsideTapped = false // default is false
public var dimAlpha: CGFloat = 0.4 // default is 0.2
public var dimBackgroundColor: UIColor? = .init(white: 0, alpha: 0.4) // default is 0.2
public var isHighlightOnButtonClicked = true
public var isDimBackgroundWhenShowing = true
public var isDismissOnOutsideTapped = false
public var dimAlpha: CGFloat = 0.4
public var dimBackgroundColor: UIColor? = .init(white: 0, alpha: 0.4)

public var appearTime = 0.3 // default is 0.2 second
public var disappearTime = 0.1 // default is 0.1 second
public var appearTime = 0.2
public var disappearTime = 0.1

public var appearType: AppearType = .default // to change the appear type
public var disappearType: DisappearType = .default // to change the disappear type
public var transitionType: TransitionType = .default

// customize the margin & spacing of title & message
public var titleSideMargin: CGFloat = 20.0 // default is 20
public var messageSideMargin: CGFloat = 20.0 // default is 20
public var titleTopMargin: CGFloat = 20.0 // default is 20
public var messageBottomMargin: CGFloat = 20.0 // default is 20
public var titleToMessageSpacing: CGFloat = 20.0 // default is 20
public var titleSideMargin: CGFloat = 20.0
public var messageSideMargin: CGFloat = 20.0
public var titleTopMargin: CGFloat = 20.0
public var messageBottomMargin: CGFloat = 20.0
public var titleToMessageSpacing: CGFloat = 20.0

// customize text fields
public var textFieldHeight: CGFloat = 34.0 // default is 32
public var textFieldSideMargin: CGFloat = 15.0 // default is 15
public var textFieldBottomMargin: CGFloat = 15.0 // default is 15
public var textFieldSpacing: CGFloat = 10.0 // default is 10
public var isFocusTextFieldWhenShowing = true // default is true
public var textFieldHeight: CGFloat = 34.0
public var textFieldSideMargin: CGFloat = 15.0
public var textFieldBottomMargin: CGFloat = 15.0
public var textFieldSpacing: CGFloat = 10.0
public var isFocusTextFieldWhenShowing = true
```

## Contributing
Expand Down
103 changes: 41 additions & 62 deletions Source/SwiftAlertView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,18 @@ public class SwiftAlertView: UIView {
case dark
}

public enum AppearType {
public enum TransitionType {
case `default`
case fadeIn
case flyFromTop
case flyFromLeft
}

public enum DisappearType {
case `default`
case fadeOut
case flyToBottom
case flyToRight
case fade
case vertical
}


// MARK: Public Properties

public weak var delegate: SwiftAlertViewDelegate? // delegate
public weak var delegate: SwiftAlertViewDelegate?

public var style: Style = .auto { // default is base on system color
public var style: Style = .auto { // default is based on system color
didSet {
updateAlertStyle()
}
Expand All @@ -64,44 +56,42 @@ public class SwiftAlertView: UIView {
public var titleLabel: UILabel! // access titleLabel to customize the title font, color
public var messageLabel: UILabel! // access messageLabel to customize the message font, color

public var cancelButtonIndex = 0 // default is 0, set this property if you want to change the position of cancel button

public var backgroundImage: UIImage?
// public var backgroundColor: UIColor? // inherits from UIView

public var cancelButtonIndex = 0 // default is 0, set this property if you want to change the position of cancel button
public var buttonTitleColor = UIColor(red: 0, green: 0.478431, blue: 1, alpha: 1) // to change the title color of all buttons
public var buttonHeight: CGFloat = 44.0 // default is 44
public var buttonHeight: CGFloat = 44.0

public var separatorColor = UIColor(red: 196.0/255, green: 196.0/255, blue: 201.0/255, alpha: 1.0) // to change the separator color
public var isHideSeparator = false // to hide the separater color
public var cornerRadius: CGFloat = 12.0 // default is 8 px
public var isHideSeparator = false
public var cornerRadius: CGFloat = 12.0

public var isDismissOnActionButtonClicked = true // default is true, if you want the alert view will not be dismissed when clicking on action buttons, set this property to false
public var isHighlightOnButtonClicked = true // default is true
public var isDimBackgroundWhenShowing = true // default is true
public var isDismissOnOutsideTapped = false // default is false
public var dimAlpha: CGFloat = 0.4 // default is 0.2
public var dimBackgroundColor: UIColor? = .init(white: 0, alpha: 0.4) // default is 0.2
public var isHighlightOnButtonClicked = true
public var isDimBackgroundWhenShowing = true
public var isDismissOnOutsideTapped = false
public var dimAlpha: CGFloat = 0.4
public var dimBackgroundColor: UIColor? = .init(white: 0, alpha: 0.4)

public var appearTime = 0.3 // default is 0.2 second
public var disappearTime = 0.1 // default is 0.1 second
public var appearTime = 0.2
public var disappearTime = 0.1

public var appearType: AppearType = .default // to change the appear type
public var disappearType: DisappearType = .default // to change the disappear type
public var transitionType: TransitionType = .default

// customize the margin & spacing of title & message
public var titleSideMargin: CGFloat = 20.0 // default is 20
public var messageSideMargin: CGFloat = 20.0 // default is 20
public var titleTopMargin: CGFloat = 20.0 // default is 20
public var messageBottomMargin: CGFloat = 20.0 // default is 20
public var titleToMessageSpacing: CGFloat = 20.0 // default is 20
public var titleSideMargin: CGFloat = 20.0
public var messageSideMargin: CGFloat = 20.0
public var titleTopMargin: CGFloat = 20.0
public var messageBottomMargin: CGFloat = 20.0
public var titleToMessageSpacing: CGFloat = 20.0

// customize text fields
public var textFieldHeight: CGFloat = 34.0 // default is 32
public var textFieldSideMargin: CGFloat = 15.0 // default is 15
public var textFieldBottomMargin: CGFloat = 15.0 // default is 15
public var textFieldSpacing: CGFloat = 10.0 // default is 10
public var isFocusTextFieldWhenShowing = true // default is true
public var textFieldHeight: CGFloat = 34.0
public var textFieldSideMargin: CGFloat = 15.0
public var textFieldBottomMargin: CGFloat = 15.0
public var textFieldSpacing: CGFloat = 10.0
public var isFocusTextFieldWhenShowing = true

// closures for handling button clicked action
public var onButtonClicked: ((_ buttonIndex: Int) -> Void)? // all buttons
Expand Down Expand Up @@ -254,7 +244,7 @@ public class SwiftAlertView: UIView {
view.addSubview(self)
view.bringSubviewToFront(self)

switch appearType {
switch transitionType {
case .default:
if isFocusTextField {
alpha = 0
Expand All @@ -277,26 +267,17 @@ public class SwiftAlertView: UIView {
self.delegate?.didPresentAlertView?(self)
}
}
case .fadeIn:
case .fade:
alpha = 0

UIView.animate(withDuration: appearTime, delay: 0, options: .curveEaseInOut) {
self.alpha = 1
} completion: { _ in
self.delegate?.didPresentAlertView?(self)
}
case .flyFromTop:
let tempFrame = frame
frame = CGRect(x: frame.origin.x, y: 0 - frame.size.height - 10, width: frame.size.width, height: frame.size.height)

UIView.animate(withDuration: appearTime, delay: 0, options: .curveEaseInOut) {
self.frame = tempFrame
} completion: { _ in
self.delegate?.didPresentAlertView?(self)
}
case .flyFromLeft:
case .vertical:
let tempFrame = frame
frame = CGRect(x: 0 - frame.size.width - 10, y: frame.origin.y, width: frame.size.width, height: frame.size.height)
frame = CGRect(x: frame.origin.x, y: superview!.frame.size.height + 10, width: frame.size.width, height: frame.size.height)

UIView.animate(withDuration: appearTime, delay: 0, options: .curveEaseInOut) {
self.frame = tempFrame
Expand All @@ -323,7 +304,7 @@ public class SwiftAlertView: UIView {
}
}

switch disappearType {
switch transitionType {
case .default:
transform = CGAffineTransform.identity

Expand All @@ -333,7 +314,7 @@ public class SwiftAlertView: UIView {
self.removeFromSuperview()
self.delegate?.didDismissAlertView?(self)
}
case .fadeOut:
case .fade:
self.alpha = 1

UIView.animate(withDuration: disappearTime, delay: 0.02, options: .curveEaseOut) {
Expand All @@ -342,20 +323,13 @@ public class SwiftAlertView: UIView {
self.removeFromSuperview()
self.delegate?.didDismissAlertView?(self)
}
case .flyToBottom:
case .vertical:
UIView.animate(withDuration: disappearTime, delay: 0.02, options: .curveEaseOut) {
self.frame = CGRect(x: self.frame.origin.x, y: self.superview!.frame.size.height + 10, width: self.frame.size.width, height: self.frame.size.height)
} completion: { _ in
self.removeFromSuperview()
self.delegate?.didDismissAlertView?(self)
}
case .flyToRight:
UIView.animate(withDuration: disappearTime, delay: 0.02, options: .curveEaseOut) {
self.frame = CGRect(x: self.superview!.frame.size.width + 10, y: self.frame.origin.y, width: self.frame.size.width, height: self.frame.size.height)
} completion: { _ in
self.removeFromSuperview()
self.delegate?.didDismissAlertView?(self)
}
}
}

Expand All @@ -365,6 +339,12 @@ public class SwiftAlertView: UIView {
handler(self, index)
}
}

public func onActionButtonClicked(_ handler: @escaping (_ alertView: SwiftAlertView, _ buttonIndex: Int) -> Void) {
self.onActionButtonClicked = { index in
handler(self, index)
}
}
}


Expand Down Expand Up @@ -416,8 +396,7 @@ extension SwiftAlertView {
cornerRadius = kDefaultCornerRadius
appearTime = kDefaultAppearTime
disappearTime = kDefaultDisappearTime
appearType = .default
disappearType = .default
transitionType = .default
buttonTitleColor = UIColor(red: 0, green: 0.478431, blue: 1, alpha: 1)
layer.cornerRadius = CGFloat(cornerRadius)
}
Expand Down
Loading

0 comments on commit 46b642e

Please sign in to comment.