如何左alignmentUISearchBar占位符文本

这是设计师希望我创建的搜索栏

我需要做一个像这样的自定义search栏。 我遇到的问题是左alignment占位符文本,以及将search图标放在右侧。 我有一个试图在UIImageView使用的search图标的PNG,并将UIImageView设置为UISearchBarUITextFieldrightView UITextField 。 这个解决scheme还没有奏效,我用完了想法。 有没有人有办法解决吗?

如果您需要进行这些自定义,请不要使用UISearchBar 。 你将不得不使用一个UITextField和一个UIImageView ,并响应委托调用。

根据Mohittomar的回答,正如@DevC所要求的那样,在占位符的末尾添加空格,这里是swift中的代码:

我在UISearchBar中设置了子类的占位符,设置了值之后,我检查最后一个字符是否是空格。 然后得到一个空间的大小和我需要添加多less个空间来左alignment。

 class SearchBar: UISearchBar, UISearchBarDelegate { override var placeholder:String? { didSet { if let text = placeholder { if text.last != " " { // get the font attribute let attr = UITextField.s_appearanceWhenContainedIn(SearchBar).defaultTextAttributes // define a max size let maxSize = CGSizeMake(UIScreen.mainScreen().bounds.size.width - 60, 40) // get the size of the text var widthText = text.boundingRectWithSize( maxSize, options: .UsesLineFragmentOrigin, attributes:attr, context:nil).size.width // get the size of one space var widthSpace = " ".boundingRectWithSize( maxSize, options: .UsesLineFragmentOrigin, attributes:attr, context:nil).size.width let spaces = floor((maxSize.width - widthText) / widthSpace) // add the spaces let newText = text + (" " * spaces) // apply the new text if nescessary if newText != text { placeholder = newText } } } } } 

Drix答案的工作解决scheme

 import Foundation import UIKit class LeftAlignedSearchBar: UISearchBar, UISearchBarDelegate { override var placeholder:String? { didSet { if #available(iOS 9.0, *) { if let text = placeholder { if text.characters.last! != " " { // get the font attribute let attr = UITextField.appearanceWhenContainedInInstancesOfClasses([LeftAlignedSearchBar.self]).defaultTextAttributes // define a max size let maxSize = CGSizeMake(UIScreen.mainScreen().bounds.size.width - 87, 40) // let maxSize = CGSizeMake(self.bounds.size.width - 92, 40) // get the size of the text let widthText = text.boundingRectWithSize( maxSize, options: .UsesLineFragmentOrigin, attributes:attr, context:nil).size.width // get the size of one space let widthSpace = " ".boundingRectWithSize( maxSize, options: .UsesLineFragmentOrigin, attributes:attr, context:nil).size.width let spaces = floor((maxSize.width - widthText) / widthSpace) // add the spaces let newText = text + ((Array(count: Int(spaces), repeatedValue: " ").joinWithSeparator(""))) // apply the new text if nescessary if newText != text { placeholder = newText } } } } } } } 

这个方法外观wEnContainedInInstancesOfClasses在iOS 8中不可用,这里有一个iOS 8的解决方法

SWIFT 3

swift 3不允许覆盖占位符属性。 这是Drix答案的修改版本。

 func setPlaceHolder(placeholder: String)-> String { var text = placeholder if text.characters.last! != " " { // define a max size let maxSize = CGSize(width: UIScreen.main.bounds.size.width - 97, height: 40) // let maxSize = CGSizeMake(self.bounds.size.width - 92, 40) // get the size of the text let widthText = text.boundingRect( with: maxSize, options: .usesLineFragmentOrigin, attributes:nil, context:nil).size.width // get the size of one space let widthSpace = " ".boundingRect( with: maxSize, options: .usesLineFragmentOrigin, attributes:nil, context:nil).size.width let spaces = floor((maxSize.width - widthText) / widthSpace) // add the spaces let newText = text + ((Array(repeating: " ", count: Int(spaces)).joined(separator: ""))) // apply the new text if nescessary if newText != text { return newText } } return placeholder; } 

并调用该函数为:

 searchBar.placeholder = self.setPlaceHolder(placeholder: "your placeholder text"); 

一个适用于Drix的swift 3解决scheme:

 import Foundation import UIKit class LeftAlignedSearchBar: UISearchBar, UISearchBarDelegate { override var placeholder:String? { didSet { if #available(iOS 9.0, *) { if let text = placeholder { if text.characters.last! != " " { // get the font attribute let attr = UITextField.appearance(whenContainedInInstancesOf: [LeftAlignedSearchBar.self]).defaultTextAttributes // define a max size let maxSize = CGSize(width: UIScreen.main.bounds.size.width - 87, height: 40) // let maxSize = CGSize(width:self.bounds.size.width - 92,height: 40) // get the size of the text let widthText = text.boundingRect( with: maxSize, options: .usesLineFragmentOrigin, attributes:attr, context:nil).size.width // get the size of one space let widthSpace = " ".boundingRect( with: maxSize, options: .usesLineFragmentOrigin, attributes:attr, context:nil).size.width let spaces = floor((maxSize.width - widthText) / widthSpace) // add the spaces let newText = text + ((Array(repeating: " ", count: Int(spaces)).joined(separator: ""))) // apply the new text if nescessary if newText != text { placeholder = newText } } } } } } /* // Only override draw() if you perform custom drawing. // An empty implementation adversely affects performance during animation. override func draw(_ rect: CGRect) { // Drawing code } */ } 

如果你希望你的控制能够完全按照你想要的方式来响应,你应该可以自己定制控制。 这个控制可以分为三个部分:

  1. 一个背景UIImageView
  2. 一个UITextField
  3. 如果您希望用户与之交互,则可以使用UIButton作为search图标

最简单的方法是创build一个新的MySearchBar类,在私有接口中有三个部分:

 @interface MySearchBar () @property (nonatomic, strong) UISearchBar* searchBar; @property (nonatomic, strong) UITextField* textField; @property (nonatomic, strong) UIButton* button; @end 

MySearchBar ,您可以创build组件,对其进行自定义,添加更好的外观和感觉。 为了取回search结果,你的控件可以有一个委托id<UISearchBarDelegate> (你的UIViewController ),它将基本上模拟一个标准的UISearchBar。

剩下的就是在控制器中创buildMySearchBar ,并将委托设置为您的视图控制器。 来自UISearchBarDelegate的消息可以在发送到你的UIViewController之前去你的MySearchBar过滤或做预处理,或者直接进入你的UIViewController

不需要任何定制就可以做到这一点…

  searchBar.placeholder=@"Search "; 

search栏的中心文本alignment方式,所以只给一些大的文本。 如果你的文字很小,那么只需在占位符后面使用一些空格。

Xamarin版本

 SearchBar.MovePlaceHolderLeft(); 

 public static void MovePlaceHolderLeft(this UISearchBar searchbar) { NSAttributedString text = new NSAttributedString(searchbar.Placeholder ?? ""); // define a max size var maxSize = new CGSize(width: UIScreen.MainScreen.Bounds.Size.Width - 97, height: 40); // get the size of the text var widthText = text.GetBoundingRect(maxSize, NSStringDrawingOptions.UsesLineFragmentOrigin, null).Size.Width; // get the size of one space var widthSpace = new NSAttributedString(" ").GetBoundingRect(maxSize, NSStringDrawingOptions.UsesLineFragmentOrigin, null).Size.Width; var spaces = Math.Floor((maxSize.Width - widthText) / widthSpace); // add the spaces string newText = searchbar.Placeholder; for (double i = 0; i < spaces; i++) { newText += " "; } searchbar.Placeholder = newText; } 

现在已经太晚了,但是如果有人仍然想知道解决scheme,那么你可以按照这个方法。

 UITextField *searchTextField = [searchBarController.searchBar valueForKey:@"_searchField"]; 

您可以使用上面的代码获取search字段。 现在只需使用你想使用的属性,如。

 searchTextField.layer.cornerRadius = 10.0f; searchTextField.textAlignment = NSTextAlignmentLeft; 

PS。 文本alignment属性用于文本和占位符。 谢谢。