Created
November 23, 2015 15:22
-
-
Save maximbilan/1a6265ffea16b3ca8d17 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import UIKit | |
@objc public class LeftAlignedSearchBar: UISearchBar, UISearchBarDelegate { | |
override public var placeholder:String? { | |
didSet { | |
if #available(iOS 9.0, *) { | |
if let text = placeholder { | |
if text.characters.last! != " " { | |
let attr = UITextField.appearanceWhenContainedInInstancesOfClasses([LeftAlignedSearchBar.self]).defaultTextAttributes | |
let maxSize = CGSizeMake(self.bounds.size.width - 86, 40) | |
let widthText = text.boundingRectWithSize( maxSize, options: .UsesLineFragmentOrigin, attributes:attr, context:nil).size.width | |
let widthSpace = " ".boundingRectWithSize( maxSize, options: .UsesLineFragmentOrigin, attributes:attr, context:nil).size.width | |
let spaces = floor((maxSize.width - widthText) / widthSpace) | |
let newText = text + ((Array(count: Int(spaces), repeatedValue: " ").joinWithSeparator(""))) | |
if newText != text { | |
placeholder = newText | |
} | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment