Skip to content

Commit

Permalink
Fixed image logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
sgr-ksmt committed Nov 30, 2017
1 parent 283ed17 commit 0d66933
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 61 deletions.
28 changes: 10 additions & 18 deletions Demo/Demo/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ class ViewController: UIViewController {
.action(.default("")) { (action, index, _) in
print(action, index)
}
.finally { (action, index, _) in
.finally { [weak self] (action, index, _) in
print(action, index)
self?.showAlertWithActionImage()
}
.show(on: self)
}
Expand Down Expand Up @@ -110,24 +111,15 @@ class ViewController: UIViewController {
}
.show()
}

private func showAlertWithActionImage() {
Alertift.alert(message: "Can use image in alert action")
.action(.default("info"), image: #imageLiteral(resourceName: "icon"))
.show(on: self)
}

private func showActionSheet(anchorView: UIView) {
// Alertift.actionSheet(message: "Which food do you like?", anchorView: anchorView)
// .actions(["🍣", "🍎", "🍖", "🍅"]) { (action, index) in
// print(action, index)
// }
// .action(.cancel("None of them"))
// .finally { action, index in
// if action.style == .cancel {
// return
// }
// Alertift.alert(message: "\(index). \(action.title!)")
// .action(.default("OK"))
// .show()
// }
// .show()
//message: "Which food do you like?"
Alertift.actionSheet(anchorView: anchorView)
.image(#imageLiteral(resourceName: "alertImage"))
Alertift.actionSheet(message: "Which food do you like?", anchorView: anchorView)
.action(.default("🍣"), image: #imageLiteral(resourceName: "icon"))
.action(.default("🍎"), image: #imageLiteral(resourceName: "icon"))
.action(.default("🍖"), image: #imageLiteral(resourceName: "icon"))
Expand Down
4 changes: 2 additions & 2 deletions Sources/ActionSheet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ extension Alertift {
return { (action, index, _) in (handler as? Handler)?(action, index) }
}

public func image(_ image: UIImage?) -> Self {
_alertController.setImage(image)
public func image(_ image: UIImage?, imageTopMargin: Alertift.ImageTopMargin = .none) -> Self {
_alertController.setImage(image, imageTopMargin: imageTopMargin)
return self
}

Expand Down
44 changes: 36 additions & 8 deletions Sources/Alert.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,41 @@ extension Alertift {
/// - handler: The block to execute after this action performed.
/// - Returns: Myself
public func action(_ action: Alertift.Action, isPreferred: Bool, handler: Handler? = nil) -> Self {
addActionToAlertController(
buildAlertAction(
action,
handler: merge(_alertController.actionWithTextFieldsHandler, handler ?? { (_, _, _)in })
),
isPreferred: isPreferred
return self.action(action, image: nil, isPreferred: isPreferred, handler: handler)
}

/// Add action to Alert
///
/// - Parameters:
/// - action: Alert action.
/// - image: Image of action.
/// - renderMode: Render mode for alert action image. Default is `.automatic`
/// - handler: The block to execute after this action performed.
/// - Returns: Myself
public func action(_ action: Alertift.Action, image: UIImage?, renderingMode: UIImageRenderingMode = .automatic, handler: Handler? = nil) -> Self {
return self.action(action, image: image, renderingMode: renderingMode, isPreferred: false, handler: handler)
}

/// Add action to Alert
///
/// - Parameters:
/// - action: Alert action.
/// - image: Image of action
/// - renderMode: Render mode for alert action image. Default is `.automatic`
/// - isPreferred: If you want to change this action to preferredAction, set true. Default is false.
/// - handler: The block to execute after this action performed.
/// - Returns: Myself
public func action(_ action: Alertift.Action, image: UIImage?, renderingMode: UIImageRenderingMode = .automatic, isPreferred: Bool, handler: Handler? = nil) -> Self {
let alertAction = buildAlertAction(
action,
handler: merge(_alertController.actionWithTextFieldsHandler, handler ?? { (_, _, _)in })
)

if let image = image {
alertAction.setValue(image.withRenderingMode(renderingMode), forKey: "image")
}

addActionToAlertController(alertAction, isPreferred: isPreferred)
return self
}

Expand Down Expand Up @@ -107,8 +135,8 @@ extension Alertift {
return { (handler as? Handler)?($0, $1, $2) }
}

public func image(_ image: UIImage?) -> Self {
_alertController.setImage(image)
public func image(_ image: UIImage?, imageTopMargin: Alertift.ImageTopMargin = .none) -> Self {
_alertController.setImage(image, imageTopMargin: imageTopMargin)
return self
}

Expand Down
65 changes: 32 additions & 33 deletions Sources/InnerAlertController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,33 @@
import Foundation
import UIKit

extension Alertift {
public enum ImageTopMargin {
case none
case belowRoundCorner

var margin: CGFloat {
switch self {
case .none: return 0.0
case .belowRoundCorner: return 16.0
}
}
}
}

/// 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 imageView: UIImageView? = nil
private var previousImgViewSize: CGSize = .zero

private var imageTopMargin: Alertift.ImageTopMargin = .none
override var title: String? {
didSet {
if title != spaceAdjustedTitle {
Expand All @@ -39,17 +52,16 @@ class InnerAlertController: UIAlertController {
}
}

public func setImage(_ image: UIImage?) {
public func setImage(_ image: UIImage?, imageTopMargin: Alertift.ImageTopMargin = .none) {
if let _ = image, title == nil {
title = " "
}
self.imageTopMargin = imageTopMargin
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
Expand All @@ -73,6 +85,10 @@ class InnerAlertController: UIAlertController {
return
}

if imageView.superview == nil {
mainView?.addSubview(imageView)
}

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
Expand All @@ -81,34 +97,30 @@ class InnerAlertController: UIAlertController {
// Adjust title if image size has changed
if previousImgViewSize != imageView.bounds.size {
previousImgViewSize = imageView.bounds.size
let info = adjustLabel(for: imageView)
adjustLabel(for: imageView)
imageView.center.x = view.bounds.width / 2.0
imageView.center.y = Constants.padding(for: preferredStyle) + info.height / 2.0
imageView.frame.origin.y = imageTopMargin.margin
}
}

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

if let label = titleLabel {
let info = getLineCountAndHeight(for: imageView, label: label)
let lines = String(repeating: "\n", count: info.lineCount)
let lineCount = getLineCount(for: imageView, label: label)
let lines = String(repeating: "\n", count: 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)
let lineCount = getLineCount(for: imageView, label: label)
let lines = String(repeating: "\n", count: lineCount)
spaceAdjustedMessage = lines + (originalMessage ?? "")
message = spaceAdjustedMessage
return info
} else {
return (0, 0.0)
}
}

private func getLineCountAndHeight(for imageView: UIImageView, label: UILabel?) -> AdjustInfo {
private func getLineCount(for imageView: UIImageView, label: UILabel?) -> Int {
guard let label = label else {
return (0, 0.0)
return 0
}
let _label = UILabel(frame: .zero)
_label.font = label.font
Expand All @@ -120,26 +132,13 @@ class InnerAlertController: UIAlertController {
_label.sizeToFit()
lineCount += 1
}
return (lineCount, _label.frame.height)
return lineCount
}

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 0d66933

Please sign in to comment.