Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is is possible to change a UISearchBar placeholder's alignment?

I am going to need to show the UISearchBar in different alignments because of different input languages.
I saw this answer, but I can't find where is it that you actually align the text to the right on a UISearchBar.

Any ideas?
Thanks!

like image 448
Ted Avatar asked Jul 09 '12 11:07

Ted


2 Answers

This is what I've done:

for (UIView *subView in self.subviews){
        if ([subView isKindOfClass:[UITextField class]]) {
            UITextField *text = (UITextField*)subView;
            text.textAlignment = UITextAlignmentRight;
            text.rightViewMode = UITextFieldViewModeAlways;
        }
    }

If you also want to move the magnifying glass icon, you can do this:

text.leftView = nil;
text.rightView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"search_icon.png"]];

But you will have to provide the search_icon.png image.

like image 178
BenB Avatar answered Nov 15 '22 13:11

BenB


Here is the solution

UITextField *searchTextField = [searchBar valueForKey:@"_searchField"];
UILabel *placeholderLabel = [searchTextField valueForKey:@"_placeholderLabel"];
[placeholderLabel setTextAlignment:NSTextAlignmentLeft];
like image 25
Burhanuddin Sunelwala Avatar answered Nov 15 '22 12:11

Burhanuddin Sunelwala