Skip to content

Commit

Permalink
Merge pull request #163 from christiancabarrocas/master
Browse files Browse the repository at this point in the history
Swift 2.2 update - Color conversion refactor
  • Loading branch information
vikmeup committed Apr 18, 2016
2 parents ed7311b + 0cbf0b6 commit 43774e0
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Example/SCLAlertViewExample/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class ViewController: UIViewController {

@IBAction func showSuccess(sender: AnyObject) {
let alert = SCLAlertView()
alert.addButton("First Button", target:self, selector:Selector("firstButton"))
alert.addButton("First Button", target:self, selector:#selector(ViewController.firstButton))
alert.addButton("Second Button") {
print("Second button tapped")
}
Expand Down
8 changes: 8 additions & 0 deletions SCLAlertView.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
9E124F1B1C9EA303001A4972 /* SCLAlertViewInitTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E124F1A1C9EA303001A4972 /* SCLAlertViewInitTests.swift */; };
9E124F1D1C9EA6B5001A4972 /* SCLTextFieldTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E124F1C1C9EA6B5001A4972 /* SCLTextFieldTests.swift */; };
9E47417F1CA1DABE00F95B05 /* SCLButtonTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9E47417E1CA1DABE00F95B05 /* SCLButtonTests.swift */; };
9EAEFBC51CC2AA6600BA87FB /* SCLExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EAEFBC41CC2AA6600BA87FB /* SCLExtensions.swift */; };
9EE073151C9D7F4C002B43FD /* SCLAlertViewPropertiesTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EE073131C9D7F4C002B43FD /* SCLAlertViewPropertiesTests.swift */; };
9EE073161C9D7F4C002B43FD /* SCLAlertViewStyleTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EE073141C9D7F4C002B43FD /* SCLAlertViewStyleTests.swift */; };
9EE073171C9D7F6B002B43FD /* SCLAlertView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 70264F171B0F592500B32B18 /* SCLAlertView.swift */; };
9EE99F911CA6B2320090F845 /* SCLPublicConstructorsTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9EE99F901CA6B2320090F845 /* SCLPublicConstructorsTest.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand All @@ -40,8 +42,10 @@
9E124F1A1C9EA303001A4972 /* SCLAlertViewInitTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SCLAlertViewInitTests.swift; sourceTree = "<group>"; };
9E124F1C1C9EA6B5001A4972 /* SCLTextFieldTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SCLTextFieldTests.swift; sourceTree = "<group>"; };
9E47417E1CA1DABE00F95B05 /* SCLButtonTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SCLButtonTests.swift; sourceTree = "<group>"; };
9EAEFBC41CC2AA6600BA87FB /* SCLExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SCLExtensions.swift; sourceTree = "<group>"; };
9EE073131C9D7F4C002B43FD /* SCLAlertViewPropertiesTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SCLAlertViewPropertiesTests.swift; sourceTree = "<group>"; };
9EE073141C9D7F4C002B43FD /* SCLAlertViewStyleTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SCLAlertViewStyleTests.swift; sourceTree = "<group>"; };
9EE99F901CA6B2320090F845 /* SCLPublicConstructorsTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SCLPublicConstructorsTest.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -89,6 +93,7 @@
isa = PBXGroup;
children = (
70264F171B0F592500B32B18 /* SCLAlertView.swift */,
9EAEFBC41CC2AA6600BA87FB /* SCLExtensions.swift */,
70264EFA1B0F588700B32B18 /* Supporting Files */,
);
path = SCLAlertView;
Expand All @@ -110,6 +115,7 @@
9EE073141C9D7F4C002B43FD /* SCLAlertViewStyleTests.swift */,
9E124F1C1C9EA6B5001A4972 /* SCLTextFieldTests.swift */,
9E47417E1CA1DABE00F95B05 /* SCLButtonTests.swift */,
9EE99F901CA6B2320090F845 /* SCLPublicConstructorsTest.swift */,
70264F071B0F588800B32B18 /* Supporting Files */,
);
path = SCLAlertViewTests;
Expand Down Expand Up @@ -231,6 +237,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
9EAEFBC51CC2AA6600BA87FB /* SCLExtensions.swift in Sources */,
70264F181B0F592500B32B18 /* SCLAlertView.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand All @@ -241,6 +248,7 @@
files = (
9E47417F1CA1DABE00F95B05 /* SCLButtonTests.swift in Sources */,
9EE073151C9D7F4C002B43FD /* SCLAlertViewPropertiesTests.swift in Sources */,
9EE99F911CA6B2320090F845 /* SCLPublicConstructorsTest.swift in Sources */,
9EE073161C9D7F4C002B43FD /* SCLAlertViewStyleTests.swift in Sources */,
9E124F1D1C9EA6B5001A4972 /* SCLTextFieldTests.swift in Sources */,
9E124F1B1C9EA303001A4972 /* SCLAlertViewInitTests.swift in Sources */,
Expand Down
22 changes: 6 additions & 16 deletions SCLAlertView/SCLAlertView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,10 @@ public class SCLAlertView: UIViewController {
viewText.textContainer.lineFragmentPadding = 0;
viewText.font = UIFont(name: kDefaultFont, size:14)
// Colours
contentView.backgroundColor = UIColorFromRGB(0xFFFFFF)
labelTitle.textColor = UIColorFromRGB(0x4D4D4D)
viewText.textColor = UIColorFromRGB(0x4D4D4D)
contentView.layer.borderColor = UIColorFromRGB(0xCCCCCC).CGColor
contentView.backgroundColor = 0xFFFFFF.toUIColor()
labelTitle.textColor = 0x4D4D4D.toUIColor()
viewText.textColor = 0x4D4D4D.toUIColor()
contentView.layer.borderColor = 0xCCCCCC.toCGColor()
//Gesture Recognizer for tapping outside the textinput
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(SCLAlertView.tapped(_:)))
tapGesture.numberOfTapsRequired = 1
Expand Down Expand Up @@ -449,7 +449,7 @@ public class SCLAlertView: UIViewController {
viewColor = UIColor()
var iconImage: UIImage?
let colorInt = colorStyle ?? style.defaultColorInt
viewColor = UIColorFromRGB(colorInt)
viewColor = colorInt.toUIColor()
// Icon style
switch style {
case .Success:
Expand Down Expand Up @@ -536,7 +536,7 @@ public class SCLAlertView: UIViewController {
}
for btn in buttons {
btn.backgroundColor = viewColor
btn.setTitleColor(UIColorFromRGB(colorTextButton ?? 0xFFFFFF), forState:UIControlState.Normal)
btn.setTitleColor(colorTextButton?.toUIColor() ?? 0xFFFFFF.toUIColor(), forState:UIControlState.Normal)
}

// Adding duration
Expand Down Expand Up @@ -589,16 +589,6 @@ public class SCLAlertView: UIViewController {
return defaultImage
}
}

// Helper function to convert from RGB to UIColor
func UIColorFromRGB(rgbValue: UInt) -> UIColor {
return UIColor(
red: CGFloat((rgbValue & 0xFF0000) >> 16) / 255.0,
green: CGFloat((rgbValue & 0x00FF00) >> 8) / 255.0,
blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
alpha: CGFloat(1.0)
)
}
}

// ------------------------------------
Expand Down
41 changes: 41 additions & 0 deletions SCLAlertView/SCLExtensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//
// SCLExtensions.swift
// SCLAlertView
//
// Created by Christian Cabarrocas on 16/04/16.
// Copyright © 2016 Alexey Poimtsev. All rights reserved.
//

import UIKit

extension Int {

func toUIColor() -> UIColor {
return UIColor(
red: CGFloat((self & 0xFF0000) >> 16) / 255.0,
green: CGFloat((self & 0x00FF00) >> 8) / 255.0,
blue: CGFloat(self & 0x0000FF) / 255.0,
alpha: CGFloat(1.0)
)
}

func toCGColor() -> CGColor {
return self.toUIColor().CGColor
}
}

extension UInt {

func toUIColor() -> UIColor {
return UIColor(
red: CGFloat((self & 0xFF0000) >> 16) / 255.0,
green: CGFloat((self & 0x00FF00) >> 8) / 255.0,
blue: CGFloat(self & 0x0000FF) / 255.0,
alpha: CGFloat(1.0)
)
}

func toCGColor() -> CGColor {
return self.toUIColor().CGColor
}
}
60 changes: 60 additions & 0 deletions SCLAlertViewTests/SCLPublicConstructorsTest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//
// SCLPublicConstructorsTest.swift
// SCLAlertView
//
// Created by Christian Cabarrocas on 26/03/16.
// Copyright © 2016 Alexey Poimtsev. All rights reserved.
//

import XCTest

class SCLPublicConstructorsTest: XCTestCase {

override func setUp() {
super.setUp()
}

override func tearDown() {
super.tearDown()
}

func testShowSuccess() {
let image = UIImage()
let alert = SCLAlertView()
let successReturn = alert.showSuccess("testTitle", subTitle: "testSubTitle", closeButtonTitle: "testClosebutton", duration: 1.0, colorStyle: 1, colorTextButton: 1, circleIconImage: image)
XCTAssertNotNil(successReturn)
}

}



/*
public func showSuccess(title: String, subTitle: String, closeButtonTitle:String?=nil, duration:NSTimeInterval=0.0, colorStyle: UInt=SCLAlertViewStyle.Success.defaultColorInt, colorTextButton: UInt=0xFFFFFF, circleIconImage: UIImage? = nil) -> SCLAlertViewResponder {
return showTitle(title, subTitle: subTitle, duration: duration, completeText:closeButtonTitle, style: .Success, colorStyle: colorStyle, colorTextButton: colorTextButton, circleIconImage: circleIconImage)
}

public class SCLAlertViewResponder {
let alertview: SCLAlertView

// Initialisation and Title/Subtitle/Close functions
public init(alertview: SCLAlertView) {
self.alertview = alertview
}

public func setTitle(title: String) {
self.alertview.labelTitle.text = title
}

public func setSubTitle(subTitle: String) {
self.alertview.viewText.text = subTitle
}

public func close() {
self.alertview.hideView()
}

public func setDismissBlock(dismissBlock: DismissBlock) {
self.alertview.dismissBlock = dismissBlock
}
*/

0 comments on commit 43774e0

Please sign in to comment.