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
Binary file modified .DS_Store
Binary file not shown.
18 changes: 18 additions & 0 deletions AutocompleteTextfieldSwift.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
objects = {

/* Begin PBXBuildFile section */
700620251DBE0B4100065B51 /* UserModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 700620241DBE0B4100065B51 /* UserModel.swift */; };
700620271DBE0B5700065B51 /* AutoCompletionTextProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 700620261DBE0B5700065B51 /* AutoCompletionTextProtocol.swift */; };
700620281DBE0EE000065B51 /* AutoCompletionTextProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 700620261DBE0B5700065B51 /* AutoCompletionTextProtocol.swift */; };
F905D60F1B419684009DA356 /* LaunchScreen.xib in Resources */ = {isa = PBXBuildFile; fileRef = F905D60B1B419684009DA356 /* LaunchScreen.xib */; };
F905D6101B419684009DA356 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = F905D60D1B419684009DA356 /* Main.storyboard */; };
F96E31911A98DC36001DCEBF /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = F96E31901A98DC36001DCEBF /* Images.xcassets */; };
Expand All @@ -31,6 +34,8 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
700620241DBE0B4100065B51 /* UserModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = UserModel.swift; path = ../UserModel.swift; sourceTree = "<group>"; };
700620261DBE0B5700065B51 /* AutoCompletionTextProtocol.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = AutoCompletionTextProtocol.swift; path = AutocompleteTextfieldSwift/AutoCompleteTextField/AutoCompletionTextProtocol.swift; sourceTree = SOURCE_ROOT; };
F905D60C1B419684009DA356 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = AutocompleteTextfieldSwift/Base.lproj/LaunchScreen.xib; sourceTree = SOURCE_ROOT; };
F905D60E1B419684009DA356 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = AutocompleteTextfieldSwift/Base.lproj/Main.storyboard; sourceTree = SOURCE_ROOT; };
F96E31841A98DC35001DCEBF /* AutocompleteTextfieldSwift.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = AutocompleteTextfieldSwift.app; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -156,7 +161,9 @@
F9DD71CD1CA3F20200B17E74 /* AutocompleteTextField */ = {
isa = PBXGroup;
children = (
700620241DBE0B4100065B51 /* UserModel.swift */,
F9DD71CE1CA3F20200B17E74 /* AutoCompleteTextField.swift */,
700620261DBE0B5700065B51 /* AutoCompletionTextProtocol.swift */,
);
path = AutocompleteTextField;
sourceTree = "<group>";
Expand Down Expand Up @@ -265,8 +272,10 @@
files = (
F99A8A3C1A99ADAF00DFD19D /* MKMapViewExtension.swift in Sources */,
F9DD71CF1CA3F20200B17E74 /* AutoCompleteTextField.swift in Sources */,
700620271DBE0B5700065B51 /* AutoCompletionTextProtocol.swift in Sources */,
F96E31B51A98DCA3001DCEBF /* ViewController.swift in Sources */,
F96E31B41A98DCA3001DCEBF /* AppDelegate.swift in Sources */,
700620251DBE0B4100065B51 /* UserModel.swift in Sources */,
F99A8A421A99CEF500DFD19D /* Location.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand All @@ -275,6 +284,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
700620281DBE0EE000065B51 /* AutoCompletionTextProtocol.swift in Sources */,
F96E31A01A98DC36001DCEBF /* AutocompleteTextfieldSwiftTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -349,6 +359,7 @@
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 2.3;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Debug;
Expand Down Expand Up @@ -384,6 +395,7 @@
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SWIFT_VERSION = 2.3;
TARGETED_DEVICE_FAMILY = "1,2";
VALIDATE_PRODUCT = YES;
};
Expand All @@ -395,11 +407,13 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_IDENTITY = "iPhone Developer";
ENABLE_BITCODE = NO;
GCC_GENERATE_TEST_COVERAGE_FILES = YES;
INFOPLIST_FILE = AutocompleteTextfieldSwift/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = mnbayan.AutocompleteTextfieldSwift;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 2.3;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Debug;
Expand All @@ -410,11 +424,13 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
CODE_SIGN_IDENTITY = "iPhone Developer";
ENABLE_BITCODE = NO;
GCC_GENERATE_TEST_COVERAGE_FILES = YES;
INFOPLIST_FILE = AutocompleteTextfieldSwift/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = mnbayan.AutocompleteTextfieldSwift;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 2.3;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Release;
Expand All @@ -435,6 +451,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "codefun.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 2.3;
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/AutocompleteTextfieldSwift.app/AutocompleteTextfieldSwift";
};
name = Debug;
Expand All @@ -451,6 +468,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = "codefun.$(PRODUCT_NAME:rfc1034identifier)";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 2.3;
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/AutocompleteTextfieldSwift.app/AutocompleteTextfieldSwift";
};
name = Release;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class AutoCompleteTextField:UITextField {
/// Holds the collection of attributed strings
private lazy var attributedAutoCompleteStrings = [NSAttributedString]()
/// Handles user selection action on autocomplete table view
public var onSelect:(String, NSIndexPath)->() = {_,_ in}
public var onSelect:(AutoCompletionTextProtocol, NSIndexPath)->() = {_,_ in}
/// Handles textfield's textchanged
public var onTextChange:(String)->() = {_ in}

Expand Down Expand Up @@ -48,8 +48,8 @@ public class AutoCompleteTextField:UITextField {
redrawTable()
}
}
/// The strings to be shown on as suggestions, setting the value of this automatically reload the tableview
public var autoCompleteStrings:[String]?{
/// The models to be shown on as suggestions, setting the value of this automatically reload the tableview
public var autoCompleteModels:[AutoCompletionTextProtocol]?{
didSet{ reload() }
}

Expand Down Expand Up @@ -82,8 +82,8 @@ public class AutoCompleteTextField:UITextField {
autoCompleteAttributes = [NSForegroundColorAttributeName:UIColor.blackColor()]
autoCompleteAttributes![NSFontAttributeName] = UIFont.boldSystemFontOfSize(12)
self.clearButtonMode = .Always
self.addTarget(self, action: "textFieldDidChange", forControlEvents: .EditingChanged)
self.addTarget(self, action: "textFieldDidEndEditing", forControlEvents: .EditingDidEnd)
self.addTarget(self, action: #selector(AutoCompleteTextField.textFieldDidChange), forControlEvents: .EditingChanged)
self.addTarget(self, action: #selector(AutoCompleteTextField.textFieldDidEndEditing), forControlEvents: .EditingDidEnd)
}

private func setupAutocompleteTable(view:UIView){
Expand Down Expand Up @@ -116,11 +116,16 @@ public class AutoCompleteTextField:UITextField {
attributedAutoCompleteStrings.removeAll(keepCapacity: false)
}

if let autoCompleteStrings = autoCompleteStrings, let autoCompleteAttributes = autoCompleteAttributes {
for i in 0..<autoCompleteStrings.count{
let str = autoCompleteStrings[i] as NSString
let range = str.rangeOfString(text!, options: .CaseInsensitiveSearch)
let attString = NSMutableAttributedString(string: autoCompleteStrings[i], attributes: attrs)
if let autoCompleteStrings = autoCompleteModels, let autoCompleteAttributes = autoCompleteAttributes {
for i in 0..<autoCompleteStrings.count {

let model = autoCompleteModels![i]
let str = model.autocompletionText()
let str2 = str as NSString

let range = str2.rangeOfString(text!, options: .CaseInsensitiveSearch)

let attString = NSMutableAttributedString(string: str, attributes: attrs)
attString.addAttributes(autoCompleteAttributes, range: range)
attributedAutoCompleteStrings.append(attString)
}
Expand All @@ -135,7 +140,7 @@ public class AutoCompleteTextField:UITextField {
}

onTextChange(text!)
if text!.isEmpty{ autoCompleteStrings = nil }
if text!.isEmpty{ autoCompleteModels = nil }
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.autoCompleteTableView?.hidden = self.hidesWhenEmpty! ? self.text!.isEmpty : false
})
Expand All @@ -150,7 +155,7 @@ public class AutoCompleteTextField:UITextField {
extension AutoCompleteTextField: UITableViewDataSource, UITableViewDelegate {

public func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return autoCompleteStrings != nil ? (autoCompleteStrings!.count > maximumAutoCompleteCount ? maximumAutoCompleteCount : autoCompleteStrings!.count) : 0
return autoCompleteModels != nil ? (autoCompleteModels!.count > maximumAutoCompleteCount ? maximumAutoCompleteCount : autoCompleteModels!.count) : 0
}

public func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
Expand All @@ -160,25 +165,27 @@ extension AutoCompleteTextField: UITableViewDataSource, UITableViewDelegate {
cell = UITableViewCell(style: .Default, reuseIdentifier: cellIdentifier)
}

let modelObject = autoCompleteModels![indexPath.row] as AutoCompletionTextProtocol

if enableAttributedText{
cell?.textLabel?.attributedText = attributedAutoCompleteStrings[indexPath.row]
}
else{
cell?.textLabel?.font = autoCompleteTextFont
cell?.textLabel?.textColor = autoCompleteTextColor
cell?.textLabel?.text = autoCompleteStrings![indexPath.row]

cell?.textLabel?.text = modelObject.autocompletionText()
}

cell?.contentView.gestureRecognizers = nil
return cell!
}

public func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell = tableView.cellForRowAtIndexPath(indexPath)

if let selectedText = cell?.textLabel?.text {
self.text = selectedText
onSelect(selectedText, indexPath)
if let modelObject = autoCompleteModels![indexPath.row] as AutoCompletionTextProtocol? {
self.text = modelObject.autocompletionText()
onSelect(modelObject, indexPath)
}

dispatch_async(dispatch_get_main_queue(), { () -> Void in
Expand All @@ -187,13 +194,13 @@ extension AutoCompleteTextField: UITableViewDataSource, UITableViewDelegate {
}

public func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
if cell.respondsToSelector("setSeparatorInset:"){
if cell.respondsToSelector(Selector("setSeparatorInset:")){
cell.separatorInset = autoCompleteSeparatorInset
}
if cell.respondsToSelector("setPreservesSuperviewLayoutMargins:"){
if cell.respondsToSelector(Selector("setPreservesSuperviewLayoutMargins:")){
cell.preservesSuperviewLayoutMargins = false
}
if cell.respondsToSelector("setLayoutMargins:"){
if cell.respondsToSelector(Selector("setLayoutMargins:")){
cell.layoutMargins = autoCompleteSeparatorInset
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// AutoCompletionTextProtocol.swift
// AutocompleteTextfieldSwift
//
// Created by Mohshin Shah on 24/10/2016.
// Copyright © 2016 mnbayan. All rights reserved.
//

import Foundation

/**
Enables an model object to be used in AutoCompletion of UITextField.
*/

public protocol AutoCompletionTextProtocol {

/**
Returns the autocompletion text
- returns: Model Auto Completion Text.
*/
func autocompletionText() -> String

}
Loading