5

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!

Community
  • 1
  • 1
Ted
  • 3,805
  • 14
  • 56
  • 98
  • For Swift 3. I found a solution here: [**Customize textfield easily**](http://stackoverflow.com/a/40105165/4593553) – Jerome Oct 18 '16 at 10:13

3 Answers3

6

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.

BenB
  • 1,522
  • 1
  • 14
  • 26
  • Btw, to get this to work with iOS 7 you need: [[self.subviews objectAtIndex:0] subviews] in the for loop instead of just self.subviews – Mike Sprague Apr 09 '14 at 17:15
3

Here is the solution

UITextField *searchTextField = [searchBar valueForKey:@"_searchField"];
UILabel *placeholderLabel = [searchTextField valueForKey:@"_placeholderLabel"];
[placeholderLabel setTextAlignment:NSTextAlignmentLeft];
Burhanuddin Sunelwala
  • 5,318
  • 3
  • 25
  • 51
0

To set attributed placeholder text to achieve left aligned placeholder, try this code...

//Step1 : Make blank attributed string
NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc] initWithString:@""];

//Step2 : Append placeholder to blank attributed string
NSString *strSearchHere = @"Search here...";
NSDictionary * attributes = [NSDictionary dictionaryWithObject:[UIColor blackColor] forKey:NSForegroundColorAttributeName];
NSAttributedString * subString = [[NSAttributedString alloc] initWithString:strSearchHere attributes:attributes];
[attributedString appendAttributedString:subString];

//Step3 : Append dummy text to blank attributed string
NSString *strLongText = @"LongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongText";
NSDictionary * attributesLong = [NSDictionary dictionaryWithObject:[UIColor clearColor] forKey:NSForegroundColorAttributeName];
NSAttributedString * subStringLong = [[NSAttributedString alloc] initWithString:strLongText attributes:attributesLong];
[attributedString appendAttributedString:subStringLong];

//Step4 : Set attributed placeholder string to searchBar textfield
UITextField *searchTextField = [searchBarr valueForKey:@"_searchField"];
searchTextField.attributedPlaceholder = attributedString;
searchTextField.leftView = nil; //To Remove Search icon
searchTextField.textAlignment = NSTextAlignmentLeft;
Jayesh Lathiya
  • 788
  • 6
  • 18