Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swift 3 Changes #47

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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.
15 changes: 14 additions & 1 deletion AutocompleteTextfieldSwift.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -207,14 +207,16 @@
attributes = {
LastSwiftMigration = 0710;
LastSwiftUpdateCheck = 0700;
LastUpgradeCheck = 0710;
LastUpgradeCheck = 0800;
ORGANIZATIONNAME = mnbayan;
TargetAttributes = {
F96E31831A98DC35001DCEBF = {
CreatedOnToolsVersion = 6.1;
LastSwiftMigration = 0800;
};
F96E31981A98DC36001DCEBF = {
CreatedOnToolsVersion = 6.1;
LastSwiftMigration = 0800;
TestTargetID = F96E31831A98DC35001DCEBF;
};
};
Expand Down Expand Up @@ -322,8 +324,10 @@
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
Expand All @@ -332,6 +336,7 @@
ENABLE_TESTABILITY = YES;
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_DYNAMIC_NO_PIC = NO;
GCC_NO_COMMON_BLOCKS = YES;
GCC_OPTIMIZATION_LEVEL = 0;
GCC_PREPROCESSOR_DEFINITIONS = (
"DEBUG=1",
Expand Down Expand Up @@ -366,15 +371,18 @@
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INFINITE_RECURSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
COPY_PHASE_STRIP = YES;
ENABLE_NS_ASSERTIONS = NO;
ENABLE_STRICT_OBJC_MSGSEND = YES;
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_NO_COMMON_BLOCKS = YES;
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
GCC_WARN_UNDECLARED_SELECTOR = YES;
Expand All @@ -384,6 +392,7 @@
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
TARGETED_DEVICE_FAMILY = "1,2";
VALIDATE_PRODUCT = YES;
};
Expand All @@ -400,6 +409,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = mnbayan.AutocompleteTextfieldSwift;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Debug;
Expand All @@ -415,6 +425,7 @@
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = mnbayan.AutocompleteTextfieldSwift;
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 3.0;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Release;
Expand All @@ -435,6 +446,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 = 3.0;
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/AutocompleteTextfieldSwift.app/AutocompleteTextfieldSwift";
};
name = Debug;
Expand All @@ -451,6 +463,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 = 3.0;
TEST_HOST = "$(BUILT_PRODUCTS_DIR)/AutocompleteTextfieldSwift.app/AutocompleteTextfieldSwift";
};
name = Release;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,47 +9,47 @@
import Foundation
import UIKit

public class AutoCompleteTextField:UITextField {
open class AutoCompleteTextField:UITextField {
/// Manages the instance of tableview
private var autoCompleteTableView:UITableView?
fileprivate var autoCompleteTableView:UITableView?
/// Holds the collection of attributed strings
private lazy var attributedAutoCompleteStrings = [NSAttributedString]()
fileprivate lazy var attributedAutoCompleteStrings = [NSAttributedString]()
/// Handles user selection action on autocomplete table view
public var onSelect:(String, NSIndexPath)->() = {_,_ in}
open var onSelect:(String, IndexPath)->() = {_,_ in}
/// Handles textfield's textchanged
public var onTextChange:(String)->() = {_ in}
open var onTextChange:(String)->() = {_ in}

/// Font for the text suggestions
public var autoCompleteTextFont = UIFont.systemFontOfSize(12)
open var autoCompleteTextFont = UIFont.systemFont(ofSize: 12)
/// Color of the text suggestions
public var autoCompleteTextColor = UIColor.blackColor()
open var autoCompleteTextColor = UIColor.black
/// Used to set the height of cell for each suggestions
public var autoCompleteCellHeight:CGFloat = 44.0
open var autoCompleteCellHeight:CGFloat = 44.0
/// The maximum visible suggestion
public var maximumAutoCompleteCount = 3
open var maximumAutoCompleteCount = 3
/// Used to set your own preferred separator inset
public var autoCompleteSeparatorInset = UIEdgeInsetsZero
open var autoCompleteSeparatorInset = UIEdgeInsets.zero
/// Shows autocomplete text with formatting
public var enableAttributedText = false
open var enableAttributedText = false
/// User Defined Attributes
public var autoCompleteAttributes:[String:AnyObject]?
open var autoCompleteAttributes:[String:AnyObject]?
/// Hides autocomplete tableview after selecting a suggestion
public var hidesWhenSelected = true
open var hidesWhenSelected = true
/// Hides autocomplete tableview when the textfield is empty
public var hidesWhenEmpty:Bool?{
open var hidesWhenEmpty:Bool?{
didSet{
assert(hidesWhenEmpty != nil, "hideWhenEmpty cannot be set to nil")
autoCompleteTableView?.hidden = hidesWhenEmpty!
autoCompleteTableView?.isHidden = hidesWhenEmpty!
}
}
/// The table view height
public var autoCompleteTableHeight:CGFloat?{
open var autoCompleteTableHeight:CGFloat?{
didSet{
redrawTable()
}
}
/// The strings to be shown on as suggestions, setting the value of this automatically reload the tableview
public var autoCompleteStrings:[String]?{
open var autoCompleteStrings:[String]?{

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing space open var autoCompleteStrings:[String]? {

didSet{ reload() }
}

Expand All @@ -65,41 +65,41 @@ public class AutoCompleteTextField:UITextField {
super.init(coder: aDecoder)
}

public override func awakeFromNib() {
open override func awakeFromNib() {
super.awakeFromNib()
commonInit()
setupAutocompleteTable(superview!)
}

public override func willMoveToSuperview(newSuperview: UIView?) {
super.willMoveToSuperview(newSuperview)
open override func willMove(toSuperview newSuperview: UIView?) {
super.willMove(toSuperview: newSuperview)
commonInit()
setupAutocompleteTable(newSuperview!)
}

private func commonInit(){
fileprivate func commonInit(){
hidesWhenEmpty = true
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)
autoCompleteAttributes = [NSForegroundColorAttributeName:UIColor.black]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess no need to mentioned UIColor just .black

autoCompleteAttributes![NSFontAttributeName] = UIFont.boldSystemFont(ofSize: 12)
self.clearButtonMode = .always
self.addTarget(self, action: #selector(AutoCompleteTextField.textFieldDidChange), for: .editingChanged)
self.addTarget(self, action: #selector(AutoCompleteTextField.textFieldDidEndEditing), for: .editingDidEnd)
}

private func setupAutocompleteTable(view:UIView){
let screenSize = UIScreen.mainScreen().bounds.size
let tableView = UITableView(frame: CGRectMake(self.frame.origin.x, self.frame.origin.y + CGRectGetHeight(self.frame), screenSize.width - (self.frame.origin.x * 2), 30.0))
fileprivate func setupAutocompleteTable(_ view:UIView){
let screenSize = UIScreen.main.bounds.size
let tableView = UITableView(frame: CGRect(x: self.frame.origin.x, y: self.frame.origin.y + self.frame.height, width: screenSize.width - (self.frame.origin.x * 2), height: 30.0))
tableView.dataSource = self
tableView.delegate = self
tableView.rowHeight = autoCompleteCellHeight
tableView.hidden = hidesWhenEmpty ?? true
tableView.isHidden = hidesWhenEmpty ?? true
view.addSubview(tableView)
autoCompleteTableView = tableView

autoCompleteTableHeight = 100.0
}

private func redrawTable(){
fileprivate func redrawTable(){
if let autoCompleteTableView = autoCompleteTableView, let autoCompleteTableHeight = autoCompleteTableHeight {
var newFrame = autoCompleteTableView.frame
newFrame.size.height = autoCompleteTableHeight
Expand All @@ -108,18 +108,18 @@ public class AutoCompleteTextField:UITextField {
}

//MARK: - Private Methods
private func reload(){
fileprivate func reload(){
if enableAttributedText{
let attrs = [NSForegroundColorAttributeName:autoCompleteTextColor, NSFontAttributeName:autoCompleteTextFont]
let attrs = [NSForegroundColorAttributeName:autoCompleteTextColor, NSFontAttributeName:autoCompleteTextFont] as [String : Any]

if attributedAutoCompleteStrings.count > 0 {
attributedAutoCompleteStrings.removeAll(keepCapacity: false)
attributedAutoCompleteStrings.removeAll(keepingCapacity: 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 range = str.range(of: text!, options: .caseInsensitive)
let attString = NSMutableAttributedString(string: autoCompleteStrings[i], attributes: attrs)
attString.addAttributes(autoCompleteAttributes, range: range)
attributedAutoCompleteStrings.append(attString)
Expand All @@ -136,69 +136,69 @@ public class AutoCompleteTextField:UITextField {

onTextChange(text!)
if text!.isEmpty{ autoCompleteStrings = nil }
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.autoCompleteTableView?.hidden = self.hidesWhenEmpty! ? self.text!.isEmpty : false
DispatchQueue.main.async(execute: { () -> Void in
self.autoCompleteTableView?.isHidden = self.hidesWhenEmpty! ? self.text!.isEmpty : false
})
}

func textFieldDidEndEditing() {
autoCompleteTableView?.hidden = true
autoCompleteTableView?.isHidden = true
}
}

//MARK: - UITableViewDataSource - UITableViewDelegate
extension AutoCompleteTextField: UITableViewDataSource, UITableViewDelegate {

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

public func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cellIdentifier = "autocompleteCellIdentifier"
var cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier)
var cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier)
if cell == nil{
cell = UITableViewCell(style: .Default, reuseIdentifier: cellIdentifier)
cell = UITableViewCell(style: .default, reuseIdentifier: cellIdentifier)
}

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it IndexPath ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please update if on swift 3. How I can i Download from pull request?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I have converted in swift 3 and implemented in my code
Thanks

}

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

public func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell = tableView.cellForRowAtIndexPath(indexPath)
public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let cell = tableView.cellForRow(at: indexPath)

if let selectedText = cell?.textLabel?.text {
self.text = selectedText
onSelect(selectedText, indexPath)
}

dispatch_async(dispatch_get_main_queue(), { () -> Void in
tableView.hidden = self.hidesWhenSelected
DispatchQueue.main.async(execute: { () -> Void in
tableView.isHidden = self.hidesWhenSelected
})
}

public func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
if cell.respondsToSelector("setSeparatorInset:"){
public func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if cell.responds(to: #selector(setter: UITableViewCell.separatorInset)){
cell.separatorInset = autoCompleteSeparatorInset
}
if cell.respondsToSelector("setPreservesSuperviewLayoutMargins:"){
if cell.responds(to: #selector(setter: UIView.preservesSuperviewLayoutMargins)){
cell.preservesSuperviewLayoutMargins = false
}
if cell.respondsToSelector("setLayoutMargins:"){
if cell.responds(to: #selector(setter: UIView.layoutMargins)){
cell.layoutMargins = autoCompleteSeparatorInset
}
}

public func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
public func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return autoCompleteCellHeight
}
}
12 changes: 6 additions & 6 deletions AutocompleteTextfieldSwift/Controllers/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,30 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?


func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
return true
}

func applicationWillResignActive(application: UIApplication) {
func applicationWillResignActive(_ application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

func applicationDidEnterBackground(application: UIApplication) {
func applicationDidEnterBackground(_ application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

func applicationWillEnterForeground(application: UIApplication) {
func applicationWillEnterForeground(_ application: UIApplication) {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

func applicationDidBecomeActive(application: UIApplication) {
func applicationDidBecomeActive(_ application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

func applicationWillTerminate(application: UIApplication) {
func applicationWillTerminate(_ application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

Expand Down
Loading