How to left align UISearchBar placeholder text

前端 未结 10 771
孤独总比滥情好
孤独总比滥情好 2020-12-17 17:04

\"This

I need to make a custom search bar like this. Th

相关标签:
10条回答
  • 2020-12-17 17:09

    Version for 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;
    }
    
    0 讨论(0)
  • 2020-12-17 17:11

    Based in Mohittomar answer, as asked by @DevC to add spaces to the end of the place holder, here the code in swift:

    I subclass placeholder in the UISearchBar, after set the value, I check if the last character is a space. then get the size of one space and how many spaces I need to add to align left.

    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
                    }
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-17 17:17

    SWIFT 3

    swift 3 does not allow to override placeholder property. This is the modified version of Drix answer.

    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;
       }
    

    and call the function as :

    searchBar.placeholder = self.setPlaceHolder(placeholder: "your placeholder text");
    
    0 讨论(0)
  • 2020-12-17 17:17

    it could be done from story board there is combo box with name semantic on attribute inspector of search bar if you set it to force right to left its done you have right align search bar and it has a similar thing for aligning from left

    0 讨论(0)
  • 2020-12-17 17:20

    It's too late, but if anyone is still wondering the solution, then you can follow this.

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

    You can get the search field using above code. Now simply use the properties you want to use, like.

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

    PS. Text alignment property is used for text and placeholder both. Thanks.

    0 讨论(0)
  • 2020-12-17 17:21

    Don't use a UISearchBar if you need to do these kinds of customizations. You'll have to make your own using a UITextField and a UIImageView, and responding to the delegate calls.

    0 讨论(0)
提交回复
热议问题