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

Fix empty placeholder crash, and remove deprecated method from iOS 13 #202

Open
wants to merge 3 commits 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
34 changes: 34 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// swift-tools-version:5.3
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
name: "SearchTextField",
platforms: [
.iOS(.v9)
],
products: [
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
name: "SearchTextField",
targets: ["SearchTextField"]),
],
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages this package depends on.
.target(
name: "SearchTextField",
dependencies: [],
path:"SearchTextField/Classes"
),
.testTarget(
name: "SearchTextFieldTests",
dependencies: ["SearchTextField"],
path:"Tests")
]
)
10 changes: 6 additions & 4 deletions SearchTextField/Classes/SearchTextField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ open class SearchTextField: UITextField {
fileprivate var timer: Timer? = nil
fileprivate var placeholderLabel: UILabel?
fileprivate static let cellIdentifier = "APSearchTextFieldCell"
fileprivate let indicator = UIActivityIndicatorView(style: .gray)
fileprivate let indicator = UIActivityIndicatorView(style: UIActivityIndicatorView.Style.medium)
fileprivate var maxTableViewSize: CGFloat = 0

fileprivate var filteredResults = [SearchTextFieldItem]()
Expand Down Expand Up @@ -253,7 +253,8 @@ open class SearchTextField: UITextField {
placeholderLabel?.backgroundColor = UIColor.clear
placeholderLabel?.lineBreakMode = .byClipping

if let placeholderColor = self.attributedPlaceholder?.attribute(NSAttributedString.Key.foregroundColor, at: 0, effectiveRange: nil) as? UIColor {
if let attributedPlaceholder = self.attributedPlaceholder, attributedPlaceholder.length > 0,
let placeholderColor = attributedPlaceholder.attribute(.foregroundColor, at: 0, effectiveRange: nil) as? UIColor {
placeholderLabel?.textColor = placeholderColor
} else {
placeholderLabel?.textColor = UIColor ( red: 0.8, green: 0.8, blue: 0.8, alpha: 1.0 )
Expand Down Expand Up @@ -530,7 +531,7 @@ open class SearchTextField: UITextField {
// MARK: - Prepare for draw table result

fileprivate func prepareDrawTableResult() {
guard let frame = self.superview?.convert(self.frame, to: UIApplication.shared.keyWindow) else { return }
guard let frame = self.superview?.convert(self.frame, to: UIApplication.shared.windows.first { $0.isKeyWindow }) else { return }
if let keyboardFrame = keyboardFrame {
var newFrame = frame
newFrame.size.height += theme.cellHeight
Expand All @@ -543,7 +544,8 @@ open class SearchTextField: UITextField {

redrawSearchTableView()
} else {
if self.center.y + theme.cellHeight > UIApplication.shared.keyWindow!.frame.size.height {
let key = UIApplication.shared.windows.first { $0.isKeyWindow }
if self.center.y + theme.cellHeight > key!.frame.size.height {
direction = .up
} else {
direction = .down
Expand Down
18 changes: 18 additions & 0 deletions Tests/SearchTextFieldTests/SearchTextFieldTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#if canImport(UIKit)
@testable import SearchTextField
import XCTest

class SearchTextFieldTest: XCTestCase {
override func setUp() {
// add setup code
}

override func tearDown() {
// add tear down code
}

func testSearchTextField() {
// add test code
}
}
#endif