On iOS 7 UISearchBar left align placeholder text?

后端 未结 2 1031
暖寄归人
暖寄归人 2021-02-19 07:20

On iOS 7 UISearchBar the placeholder text is centred and I want to disable that and make it always stick to the left like it was before.

相关标签:
2条回答
  • 2021-02-19 07:46

    I haven't tried it with a UISearchBar but this works with a UITextField. I Subclassed it and overrode leftViewRectForBounds:, textRectForBounds: and editingRectForBounds:, here is the code:

    - (CGRect)textRectForBounds:(CGRect)bounds {
      CGRect inset = CGRectMake(bounds.origin.x + kLeftTextPadding,
                            bounds.origin.y,
                            bounds.size.width - kLeftTextPadding,
                            bounds.size.height);
      return inset;
    }
    

    Hope it helps.

    0 讨论(0)
  • 2021-02-19 07:48

    To get left alignment of the placeholder in iOS7 you can add white spaces to the end of your placeholder string. For example:

    _searchBar.placeholder = @"Search                  ";
    

    Note that it is should be done separately for each search bar and each language.

    One could try to write automatic calculation of needed spaces after text for any language but it seems impossible to read the size of searchBar's textField frame

    <UISearchBarTextField: 0x1349c160; frame = (0 0; 0 0);
    
    0 讨论(0)
提交回复
热议问题