From 9971d714d04b12c0953c8a4a775d1bafa38e6e90 Mon Sep 17 00:00:00 2001 From: Hemang Date: Mon, 30 Oct 2017 16:40:28 +0530 Subject: [PATCH 1/2] Added Attributed Message Support - Added Attributed Message Support - Improved Warnings --- Source/JHTAlertController.swift | 115 ++++++++++++++++++++++++++++++-- 1 file changed, 110 insertions(+), 5 deletions(-) diff --git a/Source/JHTAlertController.swift b/Source/JHTAlertController.swift index c579310..95de403 100644 --- a/Source/JHTAlertController.swift +++ b/Source/JHTAlertController.swift @@ -142,7 +142,8 @@ public class JHTAlertController: UIViewController, UIViewControllerTransitioning } } private var message: String! - + private var messageAttributed: NSAttributedString! + // MARK: ButtonContainer private var buttonContainerView = UIStackView() @@ -215,6 +216,110 @@ public class JHTAlertController: UIViewController, UIViewControllerTransitioning private let textFieldCornerRadius: CGFloat = 4.0 // MARK:- Initialization and Setup + + required convenience public init(message: NSAttributedString, preferredStyle: JHTAlertControllerStyle, iconImage: UIImage? = nil) { + self.init(nibName: nil, bundle: nil) + + self.messageAttributed = message + self.preferredStyle = preferredStyle + + self.providesPresentationContextTransitionStyle = true + self.definesPresentationContext = true + self.modalPresentationStyle = UIModalPresentationStyle.custom + self.transitioningDelegate = self + + + // Setup ContainerView + containerView.frame.size = CGSize(width: containerViewWidth, height:300) + containerView.backgroundColor = alertBackgroundColor + containerView.translatesAutoresizingMaskIntoConstraints = false + containerView.layer.cornerRadius = cornerRadius + containerView.clipsToBounds = true + view.addSubview(containerView) + + let containerViewCenterXConstraint = NSLayoutConstraint(item: containerView, attribute: .centerX, relatedBy: .equal, toItem: view, attribute: .centerX, multiplier: 1.0, constant: 0.0) + let containerViewCenterYConstraint = NSLayoutConstraint(item: containerView, attribute: .centerY, relatedBy: .equal, toItem: view, attribute: .centerY, multiplier: 1.0, constant: 0.0) + let containerViewHeightConstraint = NSLayoutConstraint(item: containerView, attribute: .height, relatedBy: .lessThanOrEqual, toItem: view, attribute: .height, multiplier: 1.0, constant: 0.0) + let containterViewWidthConstraint = NSLayoutConstraint(item: containerView, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .width, multiplier: 1.0, constant: containerViewWidth) + view.addConstraints([containerViewCenterXConstraint, + containerViewCenterYConstraint, + containterViewWidthConstraint, + containerViewHeightConstraint]) + + // Setup Image Circle + if iconImage != nil { + iconImageView = UIImageView(image: iconImage) + view.addSubview(iconImageView!) + iconImageView!.translatesAutoresizingMaskIntoConstraints = false + + let imageCenterX = NSLayoutConstraint(item: iconImageView!, attribute: .centerX, relatedBy: .equal, toItem: view, attribute: .centerX, multiplier: 1.0, constant: 0) + let imageCenterY = NSLayoutConstraint(item: iconImageView!, attribute: .bottom, relatedBy: .equal, toItem: containerView, attribute: .top, multiplier: 1.0, constant: 5) + view.addConstraints([imageCenterX, + imageCenterY]) + } + + // Setup TitleView + titleView.frame.size = CGSize(width: containerViewWidth, height: titleViewHeight) + titleView.backgroundColor = titleViewBackgroundColor + titleView.translatesAutoresizingMaskIntoConstraints = false + titleView.clipsToBounds = true + containerView.addSubview(titleView) + + let titleViewLeadingConstraint = NSLayoutConstraint(item: titleView, attribute: .leading, relatedBy: .equal, toItem: containerView, attribute: .leading, multiplier: 1.0, constant: 0.0) + let titleViewTrailingConstraint = NSLayoutConstraint(item: titleView, attribute: .trailing, relatedBy: .equal, toItem: containerView, attribute: .trailing, multiplier: 1.0, constant: 0.0) + let titleViewTopConstriant = NSLayoutConstraint(item: titleView, attribute: .top, relatedBy: .equal, toItem: containerView, attribute: .top, multiplier: 1.0, constant: 0.0) + let titleViewHeightConstraint = NSLayoutConstraint(item: titleView, attribute: .height, relatedBy: .greaterThanOrEqual, toItem: nil, attribute: .height, multiplier: 1.0, constant: titleViewHeight) + + view.addConstraints([titleViewLeadingConstraint, + titleViewTopConstriant, + titleViewTrailingConstraint, + titleViewHeightConstraint]) + + // Setup MessageView + messageView.frame.size = CGSize(width: containerViewWidth, height: titleViewHeight) + messageView.translatesAutoresizingMaskIntoConstraints = false + messageView.clipsToBounds = true + containerView.addSubview(messageView) + let messageViewLeadingConstraint = NSLayoutConstraint(item: messageView, attribute: .leading, relatedBy: .equal, toItem: containerView, attribute: .leading, multiplier: 1.0, constant: 0.0) + let messageViewTrailingConstraint = NSLayoutConstraint(item: messageView, attribute: .trailing, relatedBy: .equal, toItem: containerView, attribute: .trailing, multiplier: 1.0, constant: 0.0) + let messageViewBottomConstraint = NSLayoutConstraint(item: messageView, attribute: .bottom, relatedBy: .lessThanOrEqual, toItem: containerView, attribute: .bottom, multiplier: 1.0, constant: 0.0) + let messageViewTopConstriant = NSLayoutConstraint(item: messageView, attribute: .top, relatedBy: .equal, toItem: titleView, attribute: .bottom, multiplier: 1.0, constant: 8.0) + view.addConstraints([messageViewLeadingConstraint, + messageViewTopConstriant, + messageViewTrailingConstraint, + messageViewBottomConstraint]) + + // Setup MessageLabel + messageLabel.frame.size = CGSize(width:containerViewWidth - 20, height:0.0) + messageLabel.translatesAutoresizingMaskIntoConstraints = false + messageLabel.numberOfLines = 0 + messageLabel.minimumScaleFactor = 0.5 + messageLabel.textAlignment = .center + messageLabel.font = messageFont + messageLabel.textColor = messageTextColor + messageLabel.attributedText = messageAttributed + messageView.addSubview(messageLabel) + + let messageLabelLeadingConstraint = NSLayoutConstraint(item: messageLabel, attribute: .leading, relatedBy: .equal, toItem: messageView, attribute: .leading, multiplier: 1.0, constant: 10.0) + let messageLabelTrailingConstraint = NSLayoutConstraint(item: messageLabel, attribute: .trailing, relatedBy: .equal, toItem: messageView, attribute: .trailing, multiplier: 1.0, constant:-10.0) + let messageLabelBottomConstraint = NSLayoutConstraint(item: messageLabel, attribute: .bottom, relatedBy: .equal, toItem: messageView, attribute: .bottom, multiplier: 1.0, constant: 0.0) + let messageLabelTopConstriant = NSLayoutConstraint(item: messageLabel, attribute: .top, relatedBy: .equal, toItem: messageView, attribute: .top, multiplier: 1.0, constant: 0.0) + view.addConstraints([messageLabelLeadingConstraint, + messageLabelTopConstriant, + messageLabelTrailingConstraint, + messageLabelBottomConstraint]) + + // Setup TextFieldContainerView + textFieldContainerView.frame.size = CGSize(width: containerViewWidth, height: 240.00) + textFieldContainerView.translatesAutoresizingMaskIntoConstraints = false + textFieldContainerView.clipsToBounds = true + textFieldContainerView.distribution = .fillEqually + textFieldContainerView.alignment = .fill + textFieldContainerView.axis = .vertical + textFieldContainerView.spacing = 4 + + containerView.addSubview(textFieldContainerView) + } /// Initialize the JHTAlertController /// @@ -259,8 +364,8 @@ public class JHTAlertController: UIViewController, UIViewControllerTransitioning view.addSubview(iconImageView!) iconImageView!.translatesAutoresizingMaskIntoConstraints = false - let imageCenterX = NSLayoutConstraint(item: iconImageView, attribute: .centerX, relatedBy: .equal, toItem: view, attribute: .centerX, multiplier: 1.0, constant: 0) - let imageCenterY = NSLayoutConstraint(item: iconImageView, attribute: .bottom, relatedBy: .equal, toItem: containerView, attribute: .top, multiplier: 1.0, constant: 5) + let imageCenterX = NSLayoutConstraint(item: iconImageView!, attribute: .centerX, relatedBy: .equal, toItem: view, attribute: .centerX, multiplier: 1.0, constant: 0) + let imageCenterY = NSLayoutConstraint(item: iconImageView!, attribute: .bottom, relatedBy: .equal, toItem: containerView, attribute: .top, multiplier: 1.0, constant: 5) view.addConstraints([imageCenterX, imageCenterY]) } @@ -446,7 +551,7 @@ public class JHTAlertController: UIViewController, UIViewControllerTransitioning heightConstraint, widthConstraint]) - let circlePath = UIBezierPath(arcCenter: CGPoint(x: shapeView.frame.maxX,y: shapeView.frame.maxY), radius: CGFloat(iconBackgroundRadius), startAngle: CGFloat(0), endAngle:CGFloat(M_PI * 2), clockwise: true) + let circlePath = UIBezierPath(arcCenter: CGPoint(x: shapeView.frame.maxX,y: shapeView.frame.maxY), radius: CGFloat(iconBackgroundRadius), startAngle: CGFloat(0), endAngle:CGFloat(Double.pi * 2), clockwise: true) shapeLayer = CAShapeLayer() shapeLayer.path = circlePath.cgPath @@ -554,7 +659,7 @@ public class JHTAlertController: UIViewController, UIViewControllerTransitioning /// /// - Parameter configurationHandler: the copletion of the textfield public func addTextFieldWithConfigurationHandler(configurationHandler: ((JHTTextField) -> Void)!) { - var textField = JHTTextField() + let textField = JHTTextField() textField.frame.size = CGSize(width: containerViewWidth, height: textFieldHeight) textField.borderStyle = textFieldBorderStyle textField.delegate = self From 3f8714b9986c8c19c5aed6193921f256815e7d14 Mon Sep 17 00:00:00 2001 From: Hemang Date: Mon, 30 Oct 2017 16:48:10 +0530 Subject: [PATCH 2/2] Converted to Swift 4.0 --- .../project.pbxproj | 16 ++++++------ Example/Pods/Pods.xcodeproj/project.pbxproj | 25 +++++++++++++++++-- Source/JHTAlertController.swift | 2 +- 3 files changed, 32 insertions(+), 11 deletions(-) diff --git a/Example/JHTAlertController.xcodeproj/project.pbxproj b/Example/JHTAlertController.xcodeproj/project.pbxproj index d039e68..9a48f4c 100644 --- a/Example/JHTAlertController.xcodeproj/project.pbxproj +++ b/Example/JHTAlertController.xcodeproj/project.pbxproj @@ -570,7 +570,7 @@ ONLY_ACTIVE_ARCH = YES; SDKROOT = iphoneos; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; - SWIFT_VERSION = 3.0; + SWIFT_VERSION = 4.0; }; name = Debug; }; @@ -610,7 +610,7 @@ MTL_ENABLE_DEBUG_INFO = NO; SDKROOT = iphoneos; SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; - SWIFT_VERSION = 3.0; + SWIFT_VERSION = 4.0; VALIDATE_PRODUCT = YES; }; name = Release; @@ -627,7 +627,7 @@ MODULE_NAME = ExampleApp; PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.demo.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; - SWIFT_VERSION = 3.0; + SWIFT_VERSION = 4.0; }; name = Debug; }; @@ -643,7 +643,7 @@ MODULE_NAME = ExampleApp; PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.demo.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; - SWIFT_VERSION = 3.0; + SWIFT_VERSION = 4.0; }; name = Release; }; @@ -664,7 +664,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; - SWIFT_VERSION = 3.0; + SWIFT_VERSION = 4.0; TEST_HOST = "$(BUILT_PRODUCTS_DIR)/JHTAlertController_Example.app/JHTAlertController_Example"; }; name = Debug; @@ -682,7 +682,7 @@ LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.$(PRODUCT_NAME:rfc1034identifier)"; PRODUCT_NAME = "$(TARGET_NAME)"; - SWIFT_VERSION = 3.0; + SWIFT_VERSION = 4.0; TEST_HOST = "$(BUILT_PRODUCTS_DIR)/JHTAlertController_Example.app/JHTAlertController_Example"; }; name = Release; @@ -709,7 +709,7 @@ PRODUCT_NAME = "$(TARGET_NAME)"; SKIP_INSTALL = YES; SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; - SWIFT_VERSION = 3.0; + SWIFT_VERSION = 4.0; TARGETED_DEVICE_FAMILY = "1,2"; VERSIONING_SYSTEM = "apple-generic"; VERSION_INFO_PREFIX = ""; @@ -736,7 +736,7 @@ PRODUCT_BUNDLE_IDENTIFIER = com.prolink.JHTAlertController; PRODUCT_NAME = "$(TARGET_NAME)"; SKIP_INSTALL = YES; - SWIFT_VERSION = 3.0; + SWIFT_VERSION = 4.0; TARGETED_DEVICE_FAMILY = "1,2"; VERSIONING_SYSTEM = "apple-generic"; VERSION_INFO_PREFIX = ""; diff --git a/Example/Pods/Pods.xcodeproj/project.pbxproj b/Example/Pods/Pods.xcodeproj/project.pbxproj index 3d9506f..84f0e87 100644 --- a/Example/Pods/Pods.xcodeproj/project.pbxproj +++ b/Example/Pods/Pods.xcodeproj/project.pbxproj @@ -316,6 +316,17 @@ attributes = { LastSwiftUpdateCheck = 0730; LastUpgradeCheck = 0700; + TargetAttributes = { + A111CBDFABA023612FC3F7A4268506FD = { + LastSwiftMigration = 0900; + }; + A275A35D4BF32CEF5B46DD3EAAC8EE31 = { + LastSwiftMigration = 0900; + }; + CA6A319198A90614CF55168F53771868 = { + LastSwiftMigration = 0900; + }; + }; }; buildConfigurationList = 2D8E8EC45A3A1A1D94AE762CB5028504 /* Build configuration list for PBXProject "Pods" */; compatibilityVersion = "Xcode 3.2"; @@ -404,7 +415,8 @@ SDKROOT = iphoneos; SKIP_INSTALL = YES; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; - SWIFT_VERSION = 3.0; + SWIFT_SWIFT3_OBJC_INFERENCE = On; + SWIFT_VERSION = 4.0; TARGETED_DEVICE_FAMILY = "1,2"; VERSIONING_SYSTEM = "apple-generic"; VERSION_INFO_PREFIX = ""; @@ -440,6 +452,8 @@ PRODUCT_NAME = Pods_JHTAlertController_Example; SDKROOT = iphoneos; SKIP_INSTALL = YES; + SWIFT_SWIFT3_OBJC_INFERENCE = On; + SWIFT_VERSION = 4.0; TARGETED_DEVICE_FAMILY = "1,2"; VERSIONING_SYSTEM = "apple-generic"; VERSION_INFO_PREFIX = ""; @@ -520,6 +534,8 @@ PRODUCT_NAME = Pods_JHTAlertController_Tests; SDKROOT = iphoneos; SKIP_INSTALL = YES; + SWIFT_SWIFT3_OBJC_INFERENCE = On; + SWIFT_VERSION = 4.0; TARGETED_DEVICE_FAMILY = "1,2"; VERSIONING_SYSTEM = "apple-generic"; VERSION_INFO_PREFIX = ""; @@ -597,6 +613,8 @@ SDKROOT = iphoneos; SKIP_INSTALL = YES; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_SWIFT3_OBJC_INFERENCE = On; + SWIFT_VERSION = 4.0; TARGETED_DEVICE_FAMILY = "1,2"; VERSIONING_SYSTEM = "apple-generic"; VERSION_INFO_PREFIX = ""; @@ -628,7 +646,8 @@ PRODUCT_NAME = JHTAlertController; SDKROOT = iphoneos; SKIP_INSTALL = YES; - SWIFT_VERSION = 3.0; + SWIFT_SWIFT3_OBJC_INFERENCE = On; + SWIFT_VERSION = 4.0; TARGETED_DEVICE_FAMILY = "1,2"; VERSIONING_SYSTEM = "apple-generic"; VERSION_INFO_PREFIX = ""; @@ -664,6 +683,8 @@ PRODUCT_NAME = Pods_JHTAlertController_Tests; SDKROOT = iphoneos; SKIP_INSTALL = YES; + SWIFT_SWIFT3_OBJC_INFERENCE = On; + SWIFT_VERSION = 4.0; TARGETED_DEVICE_FAMILY = "1,2"; VERSIONING_SYSTEM = "apple-generic"; VERSION_INFO_PREFIX = ""; diff --git a/Source/JHTAlertController.swift b/Source/JHTAlertController.swift index 95de403..88c7be0 100644 --- a/Source/JHTAlertController.swift +++ b/Source/JHTAlertController.swift @@ -610,7 +610,7 @@ public class JHTAlertController: UIViewController, UIViewControllerTransitioning /// The handler method for the action. This is where the code is executed. /// /// - Parameter sender: the UIButton that was pressed - public func buttonTapped(sender: UIButton) { + @objc public func buttonTapped(sender: UIButton) { self.dismiss(animated: true, completion: nil) sender.isSelected = true let action = buttonActions[sender.tag - 1]