Change font for a programmatically created UISearchBar - ios

I have found that the code in my AppDelegate:
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil]
setFont:[UIFont systemFontOfSize:14]];
works to change the font of a UISearchBar created from IB. No problems there.
However, if I create the UISearchBar in code as such:
UISearchBar *bar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
bar.placeholder = #"test";
[self.view addSubview:bar];
Then the above UITextField appearance code has no effect. Is there a way to change the font of a programmatically created UISearchBar?
Thank you!

Try this,It's Working Fine for iOS 5.0 and up: (iOS 7 also)
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setFont:[UIFont fontWithName:#"League Gothic" size:20]];

You can try this
UITextField *textField = [[searchBar subviews] objectAtIndex:1];
[textField setFont:[UIFont fontWithName:#"Helvetica" size:25]];
for more reference check this

You can use this
UITextField *textField = [self.searchBar valueForKey: #"_searchField"];
[textField setFont:[UIFont fontWithName:#"HelveticaNeue-Light" size:17.0]];
Hope this will help.

Related

TextField text Color change in searchbar

Please check my below code, I use one navigationbar which is consist a searchbar and searchbar have a textfield, now i want to change a text color of textfield and i did a below code but it is not working so please help to correct it.
[[UISearchBar appearanceWhenContainedInInstancesOfClasses:#[[UINavigationBar class]]]setTintColor:[UIColor whiteColor]];
[[UITextField appearanceWhenContainedInInstancesOfClasses:#[[UISearchBar class]]]setTextColor:[UIColor whiteColor]];
If you are using a native UISearchbar control then you may change text color with the code below
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setDefaultTextAttributes:#{NSForegroundColorAttributeName:[UIColor redColor]}];
Or you can do it for a specific searchBar via Identity Inspector:
Try this,
NSArray *searchBarSubViews = [[self.searchBar.subviews objectAtIndex:0] subviews];
for (UIView *view in searchBarSubViews) {
if([view isKindOfClass:[UITextField class]])
{
UITextField *txt = (UITextField*)view;
UIImageView *imgView = (UIImageView*)textField.leftView;
imgView.image = [imgView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
imgView.tintColor = [UIColor whiteColor];
txt.backgroundColor = [UIColor blackColor];
txt.textColor = [UIColor RedColor];
UIButton *btnClear = (UIButton*)[textField valueForKey:#"clearButton"];
[btnClear setImage:[btnClear.imageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate] forState:UIControlStateNormal];
btnClear.tintColor = [UIColor whiteColor];
}
}

UISearchbar placeholder dislocated

I have a UISearchbar in xib and the placeholder not located in perfect design.
See image
UITextField *txtField = [UITextField appearanceWhenContainedIn:[UISearchBar class], nil];
UIView *paddingView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 8, 20)];
paddingView.backgroundColor = [UIColor grayColor];
[txtField setLeftViewMode:UITextFieldViewModeAlways];
[txtField setLeftView:paddingView];
txtField.delegate = self;
[txtField setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
txtField.textAlignment = UITextAlignmentLeft;

UISearchBar text color when used in UISearchDisplayController

i'm trying to change a UISearchbar text color in iOS7 app. (i want the text to be white).
the search bar is attached to a searchDisplay controller.
i have tried many options, including this one:
[[UITextField appearanceWhenContainedIn:[UISearchBar class], [FVRNavigationBar class], nil] setTextColor:[UIColor whiteColor]];
also tried setTintColor:, and setBarTintColor:. Nothing works.
Note, this solutions does work when the search bar is not attached to a displayController.
FYI, not that i think it matters, but i use a background image for my search bar.
in addition, the searchbar is placed in my navigationBar.
here is my code:
[[UITextField appearanceWhenContainedIn:[UISearchBar class], [FVRNavigationBar class], nil] setTextColor:[UIColor whiteColor]];
_searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 52)];
[_searchBar setSearchFieldBackgroundImage:[IMAGE(#"searchBarBg") resizableImageWithCapInsets:UIEdgeInsetsMake(8, 8, 8, 8)] forState:UIControlStateNormal];
_displayController = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self];
_displayController.delegate = self;
_displayController.displaysSearchBarInNavigationBar = YES;
I'd appreciate anyones help. thanks!

How to change properties of UITextField inside UISearchBar

Basically I am accessing this URL
UITextField * textField= self.searchBar.textField;
Let's just say the function .textField does get the textField. I have verified this several times.
textField.adjustsFontSizeToFitWidth =true;
textField.minimumFontSize =10;
textField.enablesReturnKeyAutomatically =NO;
I followed with the code above.
Still the UITextField inside self.searchBar doesn't change font size when text is too long.
I wonder why.
Try to get your text field like this....
UITextField *textField = [searchbar valueForKey:#"_searchField"];
And then customize it according to you.
textField.adjustsFontSizeToFitWidth =true;
textField.minimumFontSize =10;
textField.enablesReturnKeyAutomatically =NO;
You should try using UIAppearance instance method when you want to change some class instance which is contained in other class instance. (Looking for the view hierarchy is the work around).
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor blueColor]];
Here it is well explained in documentation by Apple.
Try this code..This way you can change and manage properties of UITextField inside UISearchBar
for (UIView *searchBarSubview in [searchbar subviews])
{
if ([searchBarSubview conformsToProtocol:#protocol(UITextInputTraits)])
{
#try
{
[(UITextField *)searchBarSubview setBorderStyle:UITextBorderStyleRoundedRect];
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setFont:[UIFont fontWithName:#"jots" size:10]];
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] adjustsFontSizeToFitWidth];
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] enablesReturnKeyAutomatically];
}
#catch (NSException * e)
{
// ignore exception
}
}
}
Hope it helps you..

UISearchbar custom in iOS 6 and 7

I want my UISearchbar should look like this.
i've tried this:
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setFont:[UIFont fontWithName:#"Helvetica Neue" size:20]];
[self.seachBar setSearchFieldBackgroundImage:[UIImage imageNamed:#"background"] forState:UIControlStateNormal];
[self.seachBar setImage:[UIImage imageNamed:#"SearchIcon"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
[self.seachBar setBackgroundImage:[UIImage imageNamed:#"background"]];
[self.seachBar setText:#"Search"];
[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
Please guide or any tutorial.
Thanks
Try this code
for (UIView *searchBarSubview in [searchBar subviews])
{
if ([searchBarSubview conformsToProtocol:#protocol(UITextInputTraits)])
{
[[UISearchBar appearance] setSearchFieldBackgroundImage:[UIImage imageNamed:#"background"]forState:UIControlStateNormal];
[(UITextField *)searchBarSubview setBorderStyle:UITextBorderStyleNone];
[[UISearchBar appearance] setBackgroundImage:[UIImage imageNamed:#"search"]];
[[UISearchBar appearance] setTintColor:[UIColor clearColor]];
}
}
Hope, your problem solved...
refer below link it more helpful you don't waste your valuable time
Right align magnifying glass icon in UISearchBar

Resources