diff --git a/Library/PMAlertAction.swift b/Library/PMAlertAction.swift index 9271c84..1ba5a25 100644 --- a/Library/PMAlertAction.swift +++ b/Library/PMAlertAction.swift @@ -27,21 +27,32 @@ import UIKit super.init(frame: CGRect.zero) } - @objc public convenience init(title: String?, style: PMAlertActionStyle, action: (() -> Void)? = nil){ - self.init() - + private func commonInit(style: PMAlertActionStyle, action: (() -> Void)? = nil) { self.action = action self.addTarget(self, action: #selector(PMAlertAction.tapped(_:)), for: .touchUpInside) - self.setTitle(title, for: UIControl.State()) - self.titleLabel?.font = UIFont(name: "Avenir-Heavy", size: 17) - self.actionStyle = style style == .default ? (self.setTitleColor(UIColor(red: 191.0/255.0, green: 51.0/255.0, blue: 98.0/255.0, alpha: 1.0), for: UIControl.State())) : (self.setTitleColor(UIColor.gray, for: UIControl.State())) self.addSeparator() } + @objc public convenience init(title: String?, style: PMAlertActionStyle, action: (() -> Void)? = nil) { + self.init() + self.commonInit(style: style, action: action) + + self.setTitle(title, for: UIControl.State()) + self.titleLabel?.font = UIFont(name: "Avenir-Heavy", size: 17) + } + + @objc public convenience init(attributedTitle: NSAttributedString?, style: PMAlertActionStyle, action: (() -> Void)? = nil) { + self.init() + self.commonInit(style: style, action: action) + + self.setAttributedTitle(attributedTitle, for: UIControl.State()) + self.titleLabel?.font = UIFont(name: "Avenir-Heavy", size: 17) + } + required public init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } diff --git a/Library/PMAlertController.swift b/Library/PMAlertController.swift index c0d5661..8df1a1e 100755 --- a/Library/PMAlertController.swift +++ b/Library/PMAlertController.swift @@ -60,8 +60,7 @@ import UIKit //MARK: - Initialiser - @objc public convenience init(title: String?, description: String?, image: UIImage?, style: PMAlertControllerStyle) { - self.init() + private func commonInit(image: UIImage?, style: PMAlertControllerStyle) { guard let nib = loadNibAlertController(), let unwrappedView = nib[0] as? UIView else { return } self.view = unwrappedView @@ -71,6 +70,20 @@ import UIKit alertView.layer.cornerRadius = 5 (image != nil) ? (alertImage.image = image) : (headerViewHeightConstraint.constant = 0) + //if alert width = 270, else width = screen width - 36 + alertViewWidthConstraint.constant = (style == .alert) ? 270 : UIScreen.main.bounds.width - 36 + + //Gesture recognizer for background dismiss with background touch + let tapRecognizer: UITapGestureRecognizer = UITapGestureRecognizer.init(target: self, action: #selector(dismissAlertControllerFromBackgroundTap)) + alertMaskBackground.addGestureRecognizer(tapRecognizer) + + setShadowAlertView() + } + + @objc public convenience init(title: String?, description: String?, image: UIImage?, style: PMAlertControllerStyle) { + self.init() + commonInit(image: image, style: style) + if let title = title { alertTitle.text = title }else{ @@ -82,15 +95,23 @@ import UIKit }else{ alertDescription.isHidden = true } + } + + @objc public convenience init(attributedTitle: NSAttributedString?, attributedDescription: NSAttributedString?, image: UIImage?, style: PMAlertControllerStyle) { + self.init() + self.commonInit(image: image, style: style) - //if alert width = 270, else width = screen width - 36 - alertViewWidthConstraint.constant = (style == .alert) ? 270 : UIScreen.main.bounds.width - 36 - - //Gesture recognizer for background dismiss with background touch - let tapRecognizer: UITapGestureRecognizer = UITapGestureRecognizer.init(target: self, action: #selector(dismissAlertControllerFromBackgroundTap)) - alertMaskBackground.addGestureRecognizer(tapRecognizer) + if let title = attributedTitle { + alertTitle.attributedText = title + }else{ + alertTitle.isHidden = true + } - setShadowAlertView() + if let description = attributedDescription { + alertDescription.attributedText = description + }else{ + alertDescription.isHidden = true + } } //MARK: - Actions diff --git a/PMAlertControllerSample/Base.lproj/Main.storyboard b/PMAlertControllerSample/Base.lproj/Main.storyboard index 06cff21..49de1b8 100644 --- a/PMAlertControllerSample/Base.lproj/Main.storyboard +++ b/PMAlertControllerSample/Base.lproj/Main.storyboard @@ -1,11 +1,11 @@ - + - + @@ -22,11 +22,12 @@ + + + diff --git a/PMAlertControllerSample/ViewController.swift b/PMAlertControllerSample/ViewController.swift index 8332812..68b5336 100755 --- a/PMAlertControllerSample/ViewController.swift +++ b/PMAlertControllerSample/ViewController.swift @@ -130,4 +130,26 @@ class ViewController: UIViewController { self.present(alertVC, animated: true, completion: nil) } + @IBAction func showAlertWithNSAttributedTextEntry(_ sender: AnyObject) { + let attributedTitle = NSAttributedString(string: "Enter your device location", attributes: [ + NSAttributedString.Key.font: UIFont(name: "American Typewriter", size: 18.0)! + ]) + let attributedDescription = NSAttributedString(string: "If your device can't be found, you can manually enter its location, so our Sentinel Robots know where to find it", attributes: [ + NSAttributedString.Key.font: UIFont(name: "American Typewriter", size: 18.0)! + ]) + let alertVC = PMAlertController(attributedTitle: attributedTitle, attributedDescription: attributedDescription, image: UIImage(named: "flag.png"), style: .alert) + + + let cancelTitle = NSAttributedString(string: "Cancel", attributes: [ NSAttributedString.Key.font: UIFont(name: "American Typewriter", size: 18.0)! + ]) + alertVC.addAction(PMAlertAction(attributedTitle: cancelTitle, style: .cancel)) + + let okTitle = NSAttributedString(string: "Ok", attributes: [ NSAttributedString.Key.font: UIFont(name: "American Typewriter", size: 18.0)! + ]) + alertVC.addAction(PMAlertAction(attributedTitle: okTitle, style: .default, action: { () in + print (alertVC.textFields[0].text ?? "") + })) + + self.present(alertVC, animated: true, completion: nil) + } }