Skip to content

Commit

Permalink
Add underline mode, allow programmatic changes to styles
Browse files Browse the repository at this point in the history
  • Loading branch information
Fawxy committed Jul 11, 2019
1 parent 03e49db commit 6a8403f
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 120 deletions.
4 changes: 2 additions & 2 deletions CBPinEntryView.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'CBPinEntryView'
s.version = '1.6.2'
s.version = '1.7.0'
s.summary = 'A view for entering arbitrary length pins, codes or passwords written in Swift 4.2. Supports one time codes.'

# This description is used to generate tags and improve search results.
Expand All @@ -26,7 +26,7 @@ This view allows a user to enter a pin or code (for security/mobile verification
s.author = { 'Chris Byatt' => 'byatt.chris@gmail.com' }
s.source = { :git => 'https://github.com/Fawxy/CBPinEntryView.git', :tag => s.version.to_s }
s.social_media_url = 'https://twitter.com/ChrisByatt'
s.swift_version = '4.2'
s.swift_version = '5.0'

s.ios.deployment_target = '9.0'

Expand Down
43 changes: 35 additions & 8 deletions CBPinEntryView/Classes/CBPinEntryView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,17 @@ public protocol CBPinEntryViewDelegate: class {

@IBDesignable open class CBPinEntryView: UIView {

@IBInspectable open var length: Int = CBPinEntryViewDefaults.length
@IBInspectable open var length: Int = CBPinEntryViewDefaults.length {
didSet {
commonInit()
}
}

@IBInspectable open var spacing: CGFloat = CBPinEntryViewDefaults.spacing
@IBInspectable open var spacing: CGFloat = CBPinEntryViewDefaults.spacing {
didSet {
commonInit()
}
}

@IBInspectable open var entryCornerRadius: CGFloat = CBPinEntryViewDefaults.entryCornerRadius {
didSet {
Expand Down Expand Up @@ -116,9 +124,16 @@ public protocol CBPinEntryViewDelegate: class {
open var allowedEntryTypes: AllowedEntryTypes = .numerical


@IBInspectable open var isUnderlined: Bool = false
@IBInspectable open var underLineThickness: CGFloat = CBPinEntryViewDefaults.entryUnderlineThickness
@IBInspectable open var underLineColor: UIColor = CBPinEntryViewDefaults.entryUnderlineColour
@IBInspectable open var isUnderlined: Bool = false {
didSet {
commonInit()
}
}
@IBInspectable open var underLineThickness: CGFloat = CBPinEntryViewDefaults.entryUnderlineThickness {
didSet {
commonInit()
}
}

private var stackView: UIStackView?
private var textField: UITextField!
Expand Down Expand Up @@ -151,6 +166,10 @@ public protocol CBPinEntryViewDelegate: class {


private func commonInit() {
self.subviews.forEach { view in
view.removeFromSuperview()
}

setupStackView()
setupTextField()

Expand Down Expand Up @@ -194,7 +213,7 @@ public protocol CBPinEntryViewDelegate: class {

// The text could either be underlined or have a border
if isUnderlined {
button.addBottomBorder(thickness: underLineThickness, color: underLineColor)
button.addBottomBorder(thickness: underLineThickness, color: entryDefaultBorderColour)
} else {
button.layer.borderColor = entryDefaultBorderColour.cgColor
button.layer.borderWidth = entryBorderWidth
Expand Down Expand Up @@ -244,14 +263,22 @@ public protocol CBPinEntryViewDelegate: class {
if isError {
errorMode = true
for button in entryButtons {
button.layer.borderColor = entryErrorBorderColour.cgColor
button.layer.borderWidth = entryBorderWidth
if isUnderlined {
button.viewWithTag(9999)?.backgroundColor = entryErrorBorderColour
} else {
button.layer.borderColor = entryErrorBorderColour.cgColor
button.layer.borderWidth = entryBorderWidth
}
}
} else {
errorMode = false
for button in entryButtons {
if isUnderlined {
button.viewWithTag(9999)?.backgroundColor = entryDefaultBorderColour
} else {
button.layer.borderColor = entryDefaultBorderColour.cgColor
button.backgroundColor = entryBackgroundColour
}
}
}
}
Expand Down
20 changes: 9 additions & 11 deletions Example/CBPinEntryView.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -207,18 +207,18 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 0720;
LastUpgradeCheck = 1000;
LastUpgradeCheck = 1020;
ORGANIZATIONNAME = CocoaPods;
TargetAttributes = {
607FACCF1AFB9204008FA782 = {
CreatedOnToolsVersion = 6.3.1;
DevelopmentTeam = YX766DP4VF;
LastSwiftMigration = 0900;
LastSwiftMigration = 1020;
};
607FACE41AFB9204008FA782 = {
CreatedOnToolsVersion = 6.3.1;
DevelopmentTeam = YX766DP4VF;
LastSwiftMigration = 0900;
LastSwiftMigration = 1020;
TestTargetID = 607FACCF1AFB9204008FA782;
};
};
Expand Down Expand Up @@ -372,6 +372,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = YES;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
Expand Down Expand Up @@ -427,6 +428,7 @@
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_ANALYZER_LOCALIZABILITY_NONLOCALIZED = YES;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
Expand Down Expand Up @@ -483,8 +485,7 @@
MODULE_NAME = ExampleApp;
PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.demo.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
SWIFT_VERSION = 4.2;
SWIFT_VERSION = 5.0;
};
name = Debug;
};
Expand All @@ -500,8 +501,7 @@
MODULE_NAME = ExampleApp;
PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.demo.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_SWIFT3_OBJC_INFERENCE = Default;
SWIFT_VERSION = 4.2;
SWIFT_VERSION = 5.0;
};
name = Release;
};
Expand All @@ -523,8 +523,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_SWIFT3_OBJC_INFERENCE = Default;
SWIFT_VERSION = 4.2;
SWIFT_VERSION = 5.0;
};
name = Debug;
};
Expand All @@ -542,8 +541,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_SWIFT3_OBJC_INFERENCE = Default;
SWIFT_VERSION = 4.2;
SWIFT_VERSION = 5.0;
};
name = Release;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1000"
LastUpgradeVersion = "1020"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
88 changes: 8 additions & 80 deletions Example/CBPinEntryView/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="14460.31" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="vXZ-lx-hvc">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14490.70" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="vXZ-lx-hvc">
<device id="retina4_0" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14460.20"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14490.49"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
Expand Down Expand Up @@ -85,23 +85,25 @@
<action selector="pressedClear:" destination="vXZ-lx-hvc" eventType="touchUpInside" id="s4X-QJ-IXz"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="fc0-2g-qcq">
<rect key="frame" x="127" y="298" width="67" height="30"/>
<button opaque="NO" contentMode="scaleToFill" fixedFrame="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="fc0-2g-qcq">
<rect key="frame" x="127" y="403" width="67" height="30"/>
<state key="normal" title="Underline"/>
<connections>
<segue destination="JRg-yk-UTB" kind="show" identifier="Underline" id="hPK-l6-X6v"/>
<action selector="pressedUnderline:" destination="vXZ-lx-hvc" eventType="touchUpInside" id="Uxm-Yj-piF"/>
</connections>
</button>
</subviews>
<color key="backgroundColor" red="0.94509803920000002" green="0.96078431369999995" blue="0.97254901959999995" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstItem="fc0-2g-qcq" firstAttribute="top" secondItem="Gkp-B4-Rf5" secondAttribute="bottom" constant="20" id="1ZW-Ko-zca"/>
<constraint firstItem="qcc-XO-4s7" firstAttribute="centerX" secondItem="kh9-bI-dsS" secondAttribute="centerX" id="Cj4-HF-I59"/>
<constraint firstItem="l00-Oi-KOV" firstAttribute="top" secondItem="8mn-8t-3Ic" secondAttribute="bottom" constant="30" id="Fto-dd-m9g"/>
<constraint firstItem="Gkp-B4-Rf5" firstAttribute="top" secondItem="qcc-XO-4s7" secondAttribute="bottom" constant="20" id="Tct-Nx-GvP"/>
<constraint firstItem="8mn-8t-3Ic" firstAttribute="centerY" secondItem="kh9-bI-dsS" secondAttribute="centerY" multiplier="0.5" id="e15-Jp-UgT"/>
<constraint firstItem="l00-Oi-KOV" firstAttribute="centerX" secondItem="kh9-bI-dsS" secondAttribute="centerX" id="g8I-HD-8da"/>
<constraint firstItem="e7G-Qq-ufL" firstAttribute="centerX" secondItem="kh9-bI-dsS" secondAttribute="centerX" id="hkh-Ok-IeY"/>
<constraint firstItem="8mn-8t-3Ic" firstAttribute="centerX" secondItem="kh9-bI-dsS" secondAttribute="centerX" id="kSo-Zi-uTG"/>
<constraint firstItem="fc0-2g-qcq" firstAttribute="centerX" secondItem="kh9-bI-dsS" secondAttribute="centerX" id="luV-u5-TDx"/>
<constraint firstItem="Gkp-B4-Rf5" firstAttribute="centerX" secondItem="kh9-bI-dsS" secondAttribute="centerX" id="pKl-BL-O1c"/>
<constraint firstItem="e7G-Qq-ufL" firstAttribute="top" secondItem="l00-Oi-KOV" secondAttribute="bottom" constant="40" id="rQd-Pj-ZOf"/>
<constraint firstItem="qcc-XO-4s7" firstAttribute="top" secondItem="e7G-Qq-ufL" secondAttribute="bottom" constant="20" id="tyB-mB-qJl"/>
Expand All @@ -115,81 +117,7 @@
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="x5A-6p-PRh" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="1031.25" y="669.71830985915494"/>
</scene>
<!--View Controller-->
<scene sceneID="mIg-S5-jFD">
<objects>
<viewController id="JRg-yk-UTB" customClass="ViewController" customModule="CBPinEntryView_Example" customModuleProvider="target" sceneMemberID="viewController">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="RJ4-h5-JW2"/>
<viewControllerLayoutGuide type="bottom" id="geW-AH-VRk"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="zjr-k2-rsM">
<rect key="frame" x="0.0" y="0.0" width="320" height="568"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<subviews>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="B6M-3H-LFg" customClass="CBPinEntryView" customModule="CBPinEntryView">
<rect key="frame" x="75" y="122" width="170" height="40"/>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
<constraints>
<constraint firstAttribute="height" constant="40" id="91o-3N-H4c"/>
<constraint firstAttribute="width" constant="170" id="xXM-9z-Gqd"/>
</constraints>
<userDefinedRuntimeAttributes>
<userDefinedRuntimeAttribute type="number" keyPath="length">
<integer key="value" value="5"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="number" keyPath="entryCornerRadius">
<real key="value" value="3"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="color" keyPath="entryDefaultBorderColour">
<color key="value" red="0.0" green="0.47843137250000001" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="number" keyPath="keyboardType">
<integer key="value" value="0"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="boolean" keyPath="isUnderlined" value="YES"/>
<userDefinedRuntimeAttribute type="color" keyPath="underLineColor">
<color key="value" red="0.52156862749999999" green="0.52156862749999999" blue="0.52156862749999999" alpha="1" colorSpace="custom" customColorSpace="displayP3"/>
</userDefinedRuntimeAttribute>
<userDefinedRuntimeAttribute type="number" keyPath="underLineThickness">
<real key="value" value="1"/>
</userDefinedRuntimeAttribute>
</userDefinedRuntimeAttributes>
</view>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<constraints>
<constraint firstItem="B6M-3H-LFg" firstAttribute="centerX" secondItem="zjr-k2-rsM" secondAttribute="centerX" id="1Kd-sj-OQo"/>
<constraint firstItem="B6M-3H-LFg" firstAttribute="centerY" secondItem="zjr-k2-rsM" secondAttribute="centerY" multiplier="0.5" id="EEW-27-NPj"/>
</constraints>
</view>
<connections>
<outlet property="pinEntryView" destination="B6M-3H-LFg" id="0la-2R-Hm3"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="dKL-1Y-xPt" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="1815" y="670"/>
</scene>
<!--Navigation Controller-->
<scene sceneID="y4m-BQ-MR0">
<objects>
<navigationController automaticallyAdjustsScrollViewInsets="NO" id="Dse-4C-KPH" sceneMemberID="viewController">
<toolbarItems/>
<navigationBar key="navigationBar" contentMode="scaleToFill" insetsLayoutMarginsFromSafeArea="NO" id="rHm-64-UpB">
<rect key="frame" x="0.0" y="20" width="320" height="44"/>
<autoresizingMask key="autoresizingMask"/>
</navigationBar>
<nil name="viewControllers"/>
<connections>
<segue destination="vXZ-lx-hvc" kind="relationship" relationship="rootViewController" id="aPH-EK-Qxv"/>
</connections>
</navigationController>
<placeholder placeholderIdentifier="IBFirstResponder" id="Cau-T5-2yx" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="244" y="670"/>
<point key="canvasLocation" x="833" y="673"/>
</scene>
</scenes>
</document>
17 changes: 11 additions & 6 deletions Example/CBPinEntryView/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@ class ViewController: UIViewController {
}
@IBOutlet var stringOutputLabel: UILabel!

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "Underline" {
(segue.destination as! ViewController).isUnderlined = true
}
}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if #available(iOS 12, *) {
Expand All @@ -49,9 +43,20 @@ class ViewController: UIViewController {
@IBAction func pressedClear(_ sender: UIButton) {
pinEntryView.clearEntry()
}
@IBAction func pressedUnderline(_ sender: UIButton) {
if pinEntryView.isUnderlined {
pinEntryView.isUnderlined = false
} else {
pinEntryView.isUnderlined = true
}
}
}

extension ViewController: CBPinEntryViewDelegate {
func entryCompleted(with entry: String?) {
print(entry)
}

func entryChanged(_ completed: Bool) {
if completed {
print(pinEntryView.getPinAsString())
Expand Down
Loading

0 comments on commit 6a8403f

Please sign in to comment.