Skip to content

Commit

Permalink
Refactor search text field into its own subclass
Browse files Browse the repository at this point in the history
  • Loading branch information
BalestraPatrick committed May 10, 2016
1 parent 8c30732 commit 5aa3379
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 27 deletions.
12 changes: 12 additions & 0 deletions RealmVideo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
AE01EFB21CDD478000F200D3 /* RealmParser.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE01EFB11CDD478000F200D3 /* RealmParser.swift */; };
AE01EFB41CDD479300F200D3 /* Video.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE01EFB31CDD479300F200D3 /* Video.swift */; };
AE4854881CE1CB95006ADC0E /* SlidePosition.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE4854871CE1CB95006ADC0E /* SlidePosition.swift */; };
AE48548E1CE1CFF8006ADC0E /* SearchTextField.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE48548D1CE1CFF8006ADC0E /* SearchTextField.swift */; };
AE5248601CDBB05B003B7454 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE52485F1CDBB05B003B7454 /* AppDelegate.swift */; };
AE5248621CDBB05B003B7454 /* RealmVideoViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = AE5248611CDBB05B003B7454 /* RealmVideoViewController.swift */; };
AE5248651CDBB05B003B7454 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = AE5248631CDBB05B003B7454 /* Main.storyboard */; };
Expand All @@ -26,6 +27,7 @@
AE01EFB11CDD478000F200D3 /* RealmParser.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RealmParser.swift; sourceTree = "<group>"; };
AE01EFB31CDD479300F200D3 /* Video.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Video.swift; sourceTree = "<group>"; };
AE4854871CE1CB95006ADC0E /* SlidePosition.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SlidePosition.swift; sourceTree = "<group>"; };
AE48548D1CE1CFF8006ADC0E /* SearchTextField.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SearchTextField.swift; sourceTree = "<group>"; };
AE52485C1CDBB05B003B7454 /* RealmVideo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = RealmVideo.app; sourceTree = BUILT_PRODUCTS_DIR; };
AE52485F1CDBB05B003B7454 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
AE5248611CDBB05B003B7454 /* RealmVideoViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RealmVideoViewController.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -92,6 +94,14 @@
name = Utils;
sourceTree = "<group>";
};
AE48548F1CE1CFFE006ADC0E /* Views */ = {
isa = PBXGroup;
children = (
AE48548D1CE1CFF8006ADC0E /* SearchTextField.swift */,
);
name = Views;
sourceTree = "<group>";
};
AE5248531CDBB05B003B7454 = {
isa = PBXGroup;
children = (
Expand All @@ -115,6 +125,7 @@
children = (
AE52485F1CDBB05B003B7454 /* AppDelegate.swift */,
AE4854891CE1CBA8006ADC0E /* View Controllers */,
AE48548F1CE1CFFE006ADC0E /* Views */,
AE48548A1CE1CBB1006ADC0E /* Parsing */,
AE48548C1CE1CBCA006ADC0E /* Utils */,
AE48548B1CE1CBC5006ADC0E /* Model */,
Expand Down Expand Up @@ -264,6 +275,7 @@
AE5248601CDBB05B003B7454 /* AppDelegate.swift in Sources */,
AE4854881CE1CB95006ADC0E /* SlidePosition.swift in Sources */,
AE01EFB21CDD478000F200D3 /* RealmParser.swift in Sources */,
AE48548E1CE1CFF8006ADC0E /* SearchTextField.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
2 changes: 1 addition & 1 deletion RealmVideo/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@
</mask>
</variation>
</label>
<textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" placeholder="realm.io..." textAlignment="center" minimumFontSize="17" clearButtonMode="always" translatesAutoresizingMaskIntoConstraints="NO" id="CcW-Oc-KxA">
<textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="center" placeholder="realm.io..." textAlignment="center" minimumFontSize="17" clearButtonMode="always" translatesAutoresizingMaskIntoConstraints="NO" id="CcW-Oc-KxA" customClass="SearchTextField" customModule="RealmVideo" customModuleProvider="target">
<rect key="frame" x="10" y="71" width="580" height="40"/>
<constraints>
<constraint firstAttribute="height" constant="40" id="tUx-DR-9cb"/>
Expand Down
46 changes: 46 additions & 0 deletions RealmVideo/SearchTextField.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//
// SearchTextField.swift
// RealmVideo
//
// Created by Patrick Balestra on 5/10/16.
// Copyright © 2016 Patrick Balestra. All rights reserved.
//

import UIKit

@IBDesignable class SearchTextField: UITextField {

override init(frame: CGRect) {
super.init(frame: frame)
setUp()
}

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setUp()
}

func setUp() {
layer.borderColor = UIColor.whiteColor().CGColor
layer.borderWidth = 1.0
layer.cornerRadius = 5.0
tintColor = UIColor.whiteColor()

let placeholder = NSAttributedString(string: "realm.io/...", attributes: [NSForegroundColorAttributeName : UIColor(white: 1.0, alpha: 0.5)])
attributedPlaceholder = placeholder

let clearButton = UIButton(type: .Custom)
clearButton.setImage(UIImage(named: "clear-icon"), forState: .Normal)
clearButton.frame = CGRect(x: 0, y: 0, width: 25.0, height: 15.0)
clearButton.imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 10)
rightView = clearButton
rightViewMode = .Always
clearButton.addTarget(self, action: #selector(clearTextField), forControlEvents: .TouchUpInside)
}

func clearTextField() {
text = ""
}


}
33 changes: 7 additions & 26 deletions RealmVideo/WelcomeViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,36 +10,21 @@ import UIKit

class WelcomeViewController: UIViewController, UITextFieldDelegate {

@IBOutlet weak var textField: UITextField!
@IBOutlet weak var textField: SearchTextField!
@IBOutlet weak var startVideo: UIButton!
@IBOutlet var pasteClipboardButton: UIButton!
@IBOutlet var scrollView: UIScrollView!
@IBOutlet weak var pasteClipboardButton: UIButton!
@IBOutlet weak var scrollView: UIScrollView!

override func viewDidLoad() {
super.viewDidLoad()

textField.layer.borderColor = UIColor.whiteColor().CGColor
textField.layer.borderWidth = 1.0
textField.layer.cornerRadius = 5.0
textField.delegate = self
textField.tintColor = UIColor.whiteColor()
textField.becomeFirstResponder()

let placeholder = NSAttributedString(string: "realm.io/...", attributes: [NSForegroundColorAttributeName : UIColor(white: 1.0, alpha: 0.5)])
textField.attributedPlaceholder = placeholder

let clearButton = UIButton(type: .Custom)
clearButton.setImage(UIImage(named: "clear-icon"), forState: .Normal)
clearButton.frame = CGRect(x: 0, y: 0, width: 25.0, height: 15.0)
clearButton.imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 10)
textField.rightView = clearButton
textField.rightViewMode = .Always
clearButton.addTarget(self, action: #selector(clearTextField), forControlEvents: .TouchUpInside)

setValidLink("")

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(WelcomeViewController.keyboardWillShow(_:)), name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(WelcomeViewController.keyboardWillHide(_:)), name: UIKeyboardWillHideNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(keyboardWillShow), name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(keyboardWillHide), name: UIKeyboardWillHideNotification, object: nil)
}

func setValidLink(linkString: String) {
Expand Down Expand Up @@ -76,10 +61,6 @@ class WelcomeViewController: UIViewController, UITextFieldDelegate {
}
}

func clearTextField() {
textField.text = ""
}

// MARK: - UITextFieldDelegate

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
Expand All @@ -89,7 +70,7 @@ class WelcomeViewController: UIViewController, UITextFieldDelegate {

// MARK: - UIKeyboard Notifications

@objc private func keyboardWillShow(notification: NSNotification) {
func keyboardWillShow(notification: NSNotification) {
guard let keyboardInfo = notification.userInfo,
let keyboardSize = (keyboardInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.CGRectValue() else { return }

Expand All @@ -100,7 +81,7 @@ class WelcomeViewController: UIViewController, UITextFieldDelegate {
scrollView.scrollIndicatorInsets = contentInset
}

@objc private func keyboardWillHide(notification: NSNotification) {
func keyboardWillHide(notification: NSNotification) {
var contentInset = scrollView.contentInset
contentInset.bottom = 0

Expand Down

0 comments on commit 5aa3379

Please sign in to comment.