Skip to content

Commit

Permalink
Added feature of image in alert.
Browse files Browse the repository at this point in the history
  • Loading branch information
sgr-ksmt committed Nov 24, 2017
1 parent 569a1e3 commit 283ed17
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 3 deletions.
21 changes: 21 additions & 0 deletions Demo/Demo/Assets.xcassets/alertImage.imageset/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"filename" : "img2.png",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 5 additions & 2 deletions Demo/Demo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class ViewController: UIViewController {
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
Alertift.alert(title: "Alertift", message: "Alertift is swifty, modern, and awesome UIAlertController wrapper.")
.image(#imageLiteral(resourceName: "alertImage"))
.titleTextColor(.red)
.messageTextColor(.blue)
.action(.default("")) { (action, index, _) in
Expand All @@ -66,6 +67,7 @@ class ViewController: UIViewController {

private func showSimpleAlert() {
Alertift.alert(title: "Sample 1", message: "Simple alert!")
.image(#imageLiteral(resourceName: "alertImage"))
.action(.default("OK"))
.show()
}
Expand Down Expand Up @@ -123,8 +125,9 @@ class ViewController: UIViewController {
// .show()
// }
// .show()

Alertift.actionSheet(message: "Which food do you like?", anchorView: anchorView)
//message: "Which food do you like?"
Alertift.actionSheet(anchorView: anchorView)
.image(#imageLiteral(resourceName: "alertImage"))
.action(.default("🍣"), image: #imageLiteral(resourceName: "icon"))
.action(.default("🍎"), image: #imageLiteral(resourceName: "icon"))
.action(.default("🍖"), image: #imageLiteral(resourceName: "icon"))
Expand Down
7 changes: 6 additions & 1 deletion Sources/ActionSheet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,12 @@ extension Alertift {
func convertFinallyHandler(_ handler: Any) -> InnerAlertController.FinallyHandler {
return { (action, index, _) in (handler as? Handler)?(action, index) }
}


public func image(_ image: UIImage?) -> Self {
_alertController.setImage(image)
return self
}

deinit {
Debug.log()
}
Expand Down
11 changes: 11 additions & 0 deletions Sources/Alert.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
import Foundation

extension Alertift {
public enum AlertImagePosition {
case top // Above title and message
case center // Between title and message
case bottom // Below title and message
}

/// Alert
final public class Alert: AlertType, _AlertType {
public typealias Handler = (UIAlertAction, Int, [UITextField]?) -> Void
Expand Down Expand Up @@ -100,6 +106,11 @@ extension Alertift {
func convertFinallyHandler(_ handler: Any) -> InnerAlertController.FinallyHandler {
return { (handler as? Handler)?($0, $1, $2) }
}

public func image(_ image: UIImage?) -> Self {
_alertController.setImage(image)
return self
}

deinit {
Debug.log()
Expand Down
128 changes: 128 additions & 0 deletions Sources/InnerAlertController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,134 @@ import UIKit
/// InnerAlertController
/// subclass of **UIAlertController**
class InnerAlertController: UIAlertController {
typealias AdjustInfo = (lineCount: Int, height: CGFloat)

/// - Return: value that was set on `title`
private(set) var originalTitle: String?
private var spaceAdjustedTitle: String = ""
private(set) var originalMessage: String?
private var spaceAdjustedMessage: String = ""

private weak var imageView: UIImageView? = nil
private var previousImgViewSize: CGSize = .zero

override var title: String? {
didSet {
if title != spaceAdjustedTitle {
originalTitle = title
}
}
}

override var message: String? {
didSet {
if message != spaceAdjustedMessage {
originalMessage = message
}
}
}

public func setImage(_ image: UIImage?) {
if let _ = image, title == nil {
title = " "
}
guard let imageView = self.imageView else {
let imageView = UIImageView(image: image)
imageView.contentMode = .scaleAspectFill
imageView.clipsToBounds = true
self.view.addSubview(imageView)
self.imageView = imageView
imageView.alpha = 0.5
return
}
imageView.image = image
imageView.frame.size = image?.size ?? .zero
}

// MARK: - Layout code

override func viewDidLayoutSubviews() {
guard let imageView = imageView, let _ = imageView.image else {
super.viewDidLayoutSubviews()
return
}

adjustImageViewPosition()
super.viewDidLayoutSubviews()
}

private func adjustImageViewPosition() {
guard let imageView = imageView, let image = imageView.image else {
return
}

if imageView.frame.width > view.frame.width {
imageView.frame.size.width = view.frame.width
imageView.frame.size.height = imageView.frame.size.width * image.size.height / image.size.width
}

// Adjust title if image size has changed
if previousImgViewSize != imageView.bounds.size {
previousImgViewSize = imageView.bounds.size
let info = adjustLabel(for: imageView)
imageView.center.x = view.bounds.width / 2.0
imageView.center.y = Constants.padding(for: preferredStyle) + info.height / 2.0
}
}

private func adjustLabel(for imageView: UIImageView) -> AdjustInfo {

if let label = titleLabel {
let info = getLineCountAndHeight(for: imageView, label: label)
let lines = String(repeating: "\n", count: info.lineCount)
spaceAdjustedTitle = lines + (originalTitle ?? "")
title = spaceAdjustedTitle
return info
} else if let label = messageLabel {
let info = getLineCountAndHeight(for: imageView, label: label)
let lines = String(repeating: "\n", count: info.lineCount)
spaceAdjustedMessage = lines + (originalMessage ?? "")
message = spaceAdjustedMessage
return info
} else {
return (0, 0.0)
}
}

private func getLineCountAndHeight(for imageView: UIImageView, label: UILabel?) -> AdjustInfo {
guard let label = label else {
return (0, 0.0)
}
let _label = UILabel(frame: .zero)
_label.font = label.font
_label.text = ""
_label.numberOfLines = 0
var lineCount = 1
while _label.frame.height < imageView.frame.height {
_label.text = _label.text.map { $0 + "\n" }
_label.sizeToFit()
lineCount += 1
}
return (lineCount, _label.frame.height)
}

private lazy var lineHeight: CGFloat = {
return titleLabel?.font.lineHeight ?? messageLabel?.font.lineHeight ?? 1.0
}()

struct Constants {
static var paddingAlert: CGFloat = 20
static var paddingSheet: CGFloat = 10
static func padding(for style: UIAlertControllerStyle) -> CGFloat {
return style == .alert ? Constants.paddingAlert : Constants.paddingSheet
}
}






/// textFieldTextDidChangeHandler: ((UITextField, Int) -> Void)
var textFieldTextDidChangeHandler: Alertift.Alert.TextFieldHandler?

Expand Down

0 comments on commit 283ed17

Please sign in to comment.