Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions Library/PMAlertAction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
39 changes: 30 additions & 9 deletions Library/PMAlertController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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{
Expand All @@ -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
Expand Down
24 changes: 20 additions & 4 deletions PMAlertControllerSample/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14109" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14490.70" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14088"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14490.49"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
Expand All @@ -22,11 +22,12 @@
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
</imageView>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="9lY-Kd-UGp">
<rect key="frame" x="150" y="257.5" width="75" height="30"/>
<rect key="frame" x="142" y="257.5" width="91" height="30"/>
<constraints>
<constraint firstAttribute="width" relation="greaterThanOrEqual" constant="75" id="3hq-BU-ch7"/>
<constraint firstAttribute="height" constant="30" id="995-LB-zET"/>
</constraints>
<fontDescription key="fontDescription" name="AmericanTypewriter" family="American Typewriter" pointSize="17"/>
<state key="normal" title="Show Alert">
<color key="titleColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</state>
Expand Down Expand Up @@ -87,7 +88,7 @@
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="dAa-Tp-rp4">
<rect key="frame" x="71" y="447.5" width="232" height="30"/>
<rect key="frame" x="71.5" y="447.5" width="232" height="30"/>
<constraints>
<constraint firstAttribute="width" relation="greaterThanOrEqual" constant="200" id="gBq-9k-IRB"/>
<constraint firstAttribute="height" constant="30" id="ooF-jr-ABn"/>
Expand All @@ -99,14 +100,29 @@
<action selector="showWalkthroughWithCustomPaddings:" destination="BYZ-38-t0r" eventType="touchUpInside" id="QFg-3e-Krb"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="GlZ-DB-Faa">
<rect key="frame" x="81" y="485.5" width="213" height="30"/>
<constraints>
<constraint firstAttribute="width" relation="greaterThanOrEqual" constant="200" id="GVt-Lp-D87"/>
<constraint firstAttribute="height" constant="30" id="GqH-nc-8px"/>
</constraints>
<state key="normal" title="Show Alert with Attributed Text">
<color key="titleColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</state>
<connections>
<action selector="showAlertWithNSAttributedTextEntry:" destination="BYZ-38-t0r" eventType="touchUpInside" id="GTH-hn-LF3"/>
</connections>
</button>
</subviews>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstItem="aCJ-yh-itC" firstAttribute="top" secondItem="8bC-Xf-vdC" secondAttribute="top" id="5pH-xQ-WnV"/>
<constraint firstItem="9Tf-BW-og0" firstAttribute="top" secondItem="3jJ-qd-rp2" secondAttribute="bottom" constant="8" id="8Pr-Pu-5hK"/>
<constraint firstItem="3jJ-qd-rp2" firstAttribute="centerY" secondItem="8bC-Xf-vdC" secondAttribute="centerY" constant="15" id="9fd-Tk-8mu"/>
<constraint firstItem="GlZ-DB-Faa" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="AUN-pk-qSf"/>
<constraint firstItem="3jJ-qd-rp2" firstAttribute="top" secondItem="4HV-8v-2tv" secondAttribute="bottom" constant="8" id="GlX-Ff-iqn"/>
<constraint firstItem="BoA-fU-ZBo" firstAttribute="centerX" secondItem="aCJ-yh-itC" secondAttribute="centerX" id="OIZ-3n-yeZ"/>
<constraint firstItem="GlZ-DB-Faa" firstAttribute="top" secondItem="dAa-Tp-rp4" secondAttribute="bottom" constant="8" id="PZe-FS-dQT"/>
<constraint firstItem="9Tf-BW-og0" firstAttribute="centerX" secondItem="WaF-QJ-Xl8" secondAttribute="centerX" id="h6x-7g-Wc3"/>
<constraint firstAttribute="bottom" secondItem="aCJ-yh-itC" secondAttribute="bottom" id="hKX-9R-uUt"/>
<constraint firstItem="BoA-fU-ZBo" firstAttribute="top" secondItem="9Tf-BW-og0" secondAttribute="bottom" constant="8" id="hnU-1N-1OM"/>
Expand Down
22 changes: 22 additions & 0 deletions PMAlertControllerSample/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}