fix search bar placeholder

This commit is contained in:
Chris Li 2016-10-01 15:19:15 -04:00
parent 166bb815c6
commit 401bef533c
5 changed files with 39 additions and 30 deletions

View File

@ -37,7 +37,7 @@ class MainController: UIViewController {
newArticle?.addObserver(self, forKeyPath: "isBookmarked", options: .New, context: context)
}
didSet {
configureSearchBarPlaceHolder()
searchBar.articleTitle = article?.title
configureBookmarkButton()
configureUserActivity()
}
@ -163,32 +163,6 @@ class MainController: UIViewController {
bookmarkButton.customImageView?.highlighted = article?.isBookmarked ?? false
}
func configureSearchBarPlaceHolder() {
func truncatedPlaceHolderString(string: String?, searchBar: UISearchBar) -> String? {
guard let string = string else {return nil}
guard let label = searchBar.valueForKey("_searchField") as? UITextField else {return nil}
guard let labelFont = label.font else {return nil}
let preferredSize = CGSizeMake(searchBar.frame.width - 45.0, 1000)
var rect = (string as NSString).boundingRectWithSize(preferredSize, options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName: labelFont], context: nil)
var truncatedString = string as NSString
var istruncated = false
while rect.height > label.frame.height {
istruncated = true
truncatedString = truncatedString.substringToIndex(truncatedString.length - 2)
rect = truncatedString.boundingRectWithSize(preferredSize, options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName: labelFont], context: nil)
}
return truncatedString as String + (istruncated ? "..." : "")
}
if let title = article?.title {
let placeHolder = truncatedPlaceHolderString(title, searchBar: searchBar)
searchBar.placeholder = placeHolder
} else {
searchBar.placeholder = LocalizedStrings.search
}
}
func configureTableOfContents() {
guard traitCollection.horizontalSizeClass == .Regular && isShowingTableOfContents else {return}
tableOfContentsController?.headings = JSInjection.getTableOfContents(webView)

View File

@ -28,7 +28,6 @@ extension MainController {
showSearchResultController(animated: animated)
// SearchBar
searchBar.placeholder = LocalizedStrings.search
if !searchBar.isFirstResponder() {searchBar.becomeFirstResponder()}
// Show Cancel Button If Needed

View File

@ -49,7 +49,7 @@
</dict>
</array>
<key>CFBundleVersion</key>
<string>1.8.1553</string>
<string>1.8.1559</string>
<key>ITSAppUsesNonExemptEncryption</key>
<false/>
<key>LSRequiresIPhoneOS</key>

View File

@ -93,6 +93,11 @@ class CustomSearchBar: UISearchBar, UITextFieldDelegate {
// Used in v1.4
class SearchBar: UISearchBar, UISearchBarDelegate {
var searchTerm: String?
var articleTitle: String? {
didSet {
configurePlaceholder()
}
}
private var textField: UITextField {
return valueForKey("searchField") as! UITextField
@ -119,6 +124,7 @@ class SearchBar: UISearchBar, UISearchBarDelegate {
func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
text = searchTerm
configurePlaceholder()
Controllers.shared.main.showSearch(animated: true)
let dispatchTime: dispatch_time_t = dispatch_time(DISPATCH_TIME_NOW, Int64(0.05 * Double(NSEC_PER_SEC)))
dispatch_after(dispatchTime, dispatch_get_main_queue(), { [unowned self] in
@ -126,6 +132,10 @@ class SearchBar: UISearchBar, UISearchBarDelegate {
})
}
func searchBarTextDidEndEditing(searchBar: UISearchBar) {
configurePlaceholder()
}
func searchBarCancelButtonClicked(searchBar: UISearchBar) {
Controllers.shared.main.hideSearch(animated: true)
}
@ -138,4 +148,30 @@ class SearchBar: UISearchBar, UISearchBarDelegate {
func searchBarSearchButtonClicked(searchBar: UISearchBar) {
Controllers.search.searchResultController?.selectFirstResultIfPossible()
}
// MARK: - Helper
private func configurePlaceholder() {
if textField.editing {
placeholder = LocalizedStrings.search
} else {
placeholder = articleTitle ?? LocalizedStrings.search
}
}
private func truncatedPlaceHolderString(string: String?, searchBar: UISearchBar) -> String? {
guard let string = string,
let labelFont = textField.font else {return nil}
let preferredSize = CGSizeMake(searchBar.frame.width - 45.0, 1000)
var rect = (string as NSString).boundingRectWithSize(preferredSize, options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName: labelFont], context: nil)
var truncatedString = string as NSString
var istruncated = false
while rect.height > textField.frame.height {
istruncated = true
truncatedString = truncatedString.substringToIndex(truncatedString.length - 2)
rect = truncatedString.boundingRectWithSize(preferredSize, options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName: labelFont], context: nil)
}
return truncatedString as String + (istruncated ? "..." : "")
}
}

View File

@ -21,7 +21,7 @@
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>1.8.1553</string>
<string>1.8.1559</string>
<key>NSExtension</key>
<dict>
<key>NSExtensionMainStoryboard</key>