Skip to content

Commit

Permalink
+ Adding custom Background and Text Color for Buttons
Browse files Browse the repository at this point in the history
+ Adding showDurationStatus to Buttons
  • Loading branch information
ChristianSteffens committed May 2, 2016
1 parent 57d1089 commit e08939e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 16 deletions.
7 changes: 6 additions & 1 deletion Example/SCLAlertViewExample/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,12 @@ class ViewController: UIViewController {
print("Logged in")
}

alert.showInfo("Login", subTitle: "")
// Add Button with Duration Status and custom Colors
alert.addButton("Duration Button", backgroundColor: UIColor.brownColor(), textColor: UIColor.yellowColor(), showDurationStatus: true) {
print("Duration Button tapped")
}

alert.showInfo("Login", subTitle: "", duration: 10)
}

func firstButton() {
Expand Down
48 changes: 33 additions & 15 deletions SCLAlertView/SCLAlertView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ public class SCLButton: UIButton {
var target:AnyObject!
var selector:Selector!
var action:(()->Void)!
var customBackgroundColor:UIColor?
var customTextColor:UIColor?
var initialTitle:String!
var showDurationStatus:Bool=false

public init() {
super.init(frame: CGRectZero)
Expand Down Expand Up @@ -137,7 +141,6 @@ public class SCLAlertView: UIViewController {
var circleView = UIView()
var circleIconView : UIView?
var duration: NSTimeInterval!
var durationTitle: String!
var durationStatusTimer: NSTimer!
var durationTimer: NSTimer!
var dismissBlock : DismissBlock?
Expand Down Expand Up @@ -330,8 +333,8 @@ public class SCLAlertView: UIViewController {
return txt
}

public func addButton(title:String, action:()->Void)->SCLButton {
let btn = addButton(title)
public func addButton(title:String, backgroundColor:UIColor? = nil, textColor:UIColor? = nil, showDurationStatus:Bool=false, action:()->Void)->SCLButton {
let btn = addButton(title, backgroundColor: backgroundColor, textColor: textColor, showDurationStatus: showDurationStatus)
btn.actionType = SCLActionType.Closure
btn.action = action
btn.addTarget(self, action:#selector(SCLAlertView.buttonTapped(_:)), forControlEvents:.TouchUpInside)
Expand All @@ -340,8 +343,8 @@ public class SCLAlertView: UIViewController {
return btn
}

public func addButton(title:String, target:AnyObject, selector:Selector)->SCLButton {
let btn = addButton(title)
public func addButton(title:String, backgroundColor:UIColor? = nil, textColor:UIColor? = nil, showDurationStatus:Bool = false, target:AnyObject, selector:Selector)->SCLButton {
let btn = addButton(title, backgroundColor: backgroundColor, textColor: textColor, showDurationStatus: showDurationStatus)
btn.actionType = SCLActionType.Selector
btn.target = target
btn.selector = selector
Expand All @@ -351,14 +354,18 @@ public class SCLAlertView: UIViewController {
return btn
}

private func addButton(title:String)->SCLButton {
private func addButton(title:String, backgroundColor:UIColor? = nil, textColor:UIColor? = nil, showDurationStatus:Bool=false)->SCLButton {
// Update view height
kWindowHeight += kButtonHeight
// Add button
let btn = SCLButton()
btn.layer.masksToBounds = true
btn.setTitle(title, forState: .Normal)
btn.titleLabel?.font = UIFont(name:kButtonFont, size: 14)
btn.customBackgroundColor = backgroundColor
btn.customTextColor = textColor
btn.initialTitle = title
btn.showDurationStatus = showDurationStatus
contentView.addSubview(btn)
buttons.append(btn)
return btn
Expand Down Expand Up @@ -576,18 +583,30 @@ public class SCLAlertView: UIViewController {
}

for btn in buttons {
btn.backgroundColor = viewColor
btn.setTitleColor(UIColorFromRGB(colorTextButton ?? 0xFFFFFF), forState:UIControlState.Normal)
if let customBackgroundColor = btn.customBackgroundColor {
// Custom BackgroundColor set
btn.backgroundColor = customBackgroundColor
} else {
// Use default BackgroundColor derived from AlertStyle
btn.backgroundColor = viewColor
}

if let customTextColor = btn.customTextColor {
// Custom TextColor set
btn.setTitleColor(customTextColor, forState:UIControlState.Normal)
} else {
// Use default BackgroundColor derived from AlertStyle
btn.setTitleColor(UIColorFromRGB(colorTextButton ?? 0xFFFFFF), forState:UIControlState.Normal)
}
}

// Adding duration
if duration > 0 {
self.duration = duration
self.durationTitle = completeText ?? "Done"
durationTimer?.invalidate()
durationTimer = NSTimer.scheduledTimerWithTimeInterval(self.duration, target: self, selector: #selector(SCLAlertView.hideView), userInfo: nil, repeats: false)
durationStatusTimer?.invalidate()
durationStatusTimer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: #selector(SCLAlertView.updateStatusView), userInfo: nil, repeats: true)
durationStatusTimer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: #selector(SCLAlertView.updateDurationStatus), userInfo: nil, repeats: true)
}

// Animate in the alert view
Expand All @@ -604,11 +623,10 @@ public class SCLAlertView: UIViewController {
return SCLAlertViewResponder(alertview: self)
}

public func updateStatusView() {

if let btn = buttons.last {
duration = duration.advancedBy(-1)
let txt = "\(durationTitle) (\(duration))"
public func updateDurationStatus() {
duration = duration.advancedBy(-1)
for btn in buttons.filter({$0.showDurationStatus}) {
let txt = "\(btn.initialTitle) (\(duration))"
btn.setTitle(txt, forState: .Normal)
}
}
Expand Down

0 comments on commit e08939e

Please sign in to comment.