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: #201 Not visible when text is too long #206

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
3 changes: 3 additions & 0 deletions Example/SearchTextField/MainViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ class MainViewController: UITableViewController {
// Start visible even without user's interaction as soon as created - Default: false
countryTextField.startVisibleWithoutInteraction = true

// Enable multiline cells - Default: false
// countryTextField.enableMultiline = true

// Set data source
let countries = localCountries()
countryTextField.filterStrings(countries)
Expand Down
51 changes: 50 additions & 1 deletion SearchTextField/Classes/SearchTextField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ open class SearchTextField: UITextField {
/// How long to wait before deciding typing has stopped
open var typingStoppedDelay = 0.8

/// Enable multiline of each row text
open var enableMultiline = false

/// Set your custom visual theme, or just choose between pre-defined SearchTextFieldTheme.lightTheme() and SearchTextFieldTheme.darkTheme() themes
open var theme = SearchTextFieldTheme.lightTheme() {
didSet {
Expand Down Expand Up @@ -550,6 +553,15 @@ open class SearchTextField: UITextField {
}
}
}


// MARK: - Height for label

fileprivate func getHeight(forText: String,forWidth: CGFloat ) -> CGFloat {
let tempLabel = UILabel.init()
tempLabel.text = forText
return tempLabel.textHeight(withWidth: forWidth)
}
}

extension SearchTextField: UITableViewDelegate, UITableViewDataSource {
Expand All @@ -575,6 +587,7 @@ extension SearchTextField: UITableViewDelegate, UITableViewDataSource {
cell!.layoutMargins = UIEdgeInsets.zero
cell!.preservesSuperviewLayoutMargins = false
cell!.textLabel?.font = theme.font
cell!.textLabel?.numberOfLines = enableMultiline ? 0 : 1
cell!.detailTextLabel?.font = UIFont(name: theme.font.fontName, size: theme.font.pointSize * fontConversionRate)
cell!.textLabel?.textColor = theme.fontColor
cell!.detailTextLabel?.textColor = theme.subtitleFontColor
Expand All @@ -592,7 +605,7 @@ extension SearchTextField: UITableViewDelegate, UITableViewDataSource {
}

public func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return theme.cellHeight
return getHeight(forText: filteredResults[(indexPath as NSIndexPath).row].title, forWidth: tableView.frame.width) > theme.cellHeight && enableMultiline ? UITableView.automaticDimension : theme.cellHeight
}

public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
Expand Down Expand Up @@ -678,3 +691,39 @@ enum Direction {
case down
case up
}


////////////////////////////////////////////////////////////////////////
// Extension for UILable height calculation

extension UILabel {
func textHeight(withWidth width: CGFloat) -> CGFloat {
guard let text = text else {
return 0
}
return text.height(withWidth: width, font: font)
}

func attributedTextHeight(withWidth width: CGFloat) -> CGFloat {
guard let attributedText = attributedText else {
return 0
}
return attributedText.height(withWidth: width)
}
}

extension String {
func height(withWidth width: CGFloat, font: UIFont) -> CGFloat {
let maxSize = CGSize(width: width, height: CGFloat.greatestFiniteMagnitude)
let actualSize = self.boundingRect(with: maxSize, options: [.usesLineFragmentOrigin], attributes: [.font : font], context: nil)
return actualSize.height
}
}

extension NSAttributedString {
func height(withWidth width: CGFloat) -> CGFloat {
let maxSize = CGSize(width: width, height: CGFloat.greatestFiniteMagnitude)
let actualSize = boundingRect(with: maxSize, options: [.usesLineFragmentOrigin], context: nil)
return actualSize.height
}
}