Zmiana rozmiaru czcionki paska UISearchBar
Jak mogę zmienić rozmiar czcionki UISearchBar
?
Edit:
Odpowiedź
for(int i =0; i<[searchbar.subviews count]; i++) {
if([[searchbar.subviews objectAtIndex:i] isKindOfClass:[UITextField class]])
[(UITextField*)[searchbar.subviews objectAtIndex:i] setFont:[UIFont fontWithName:@"Helvetica" size:12]];
}
Thanks
Pankaj
14 answers
Pasek Uisearch ma w środku pole UITextField, ale nie ma do niego dostępu. Nie ma więc sposobu na zrobienie tego w standardowy sposób.
Ale istnieje nie-stardardowy sposób obejścia tego. Pasek UISearchBar dziedziczy z UIView, możesz uzyskać dostęp do jego podwidów za pomocą [SEARCHBAR subview]. Jeśli wydrukujesz w konsoli to podwidywacze, zobaczysz, że mają one te widoki.
UISearchBarBackground, UISearchBarTextField.
Więc, aby zmienić rozmiar czcionki trzeba tylko zrobić że
UITextField *textField = [[searchBar subviews] objectAtIndex:1];
[textField setFont:[UIFont fontWithName:@"Helvetica" size:40]];
Ale jeśli pasek UISearchBar zmieni się w przyszłości, a pole tekstowe nie będzie już w pozycji 1, twój program ulegnie awarii, więc lepiej sprawdzić w podglądzie pozycję pola UITextField, a następnie ustawić rozmiar czcionki.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-01-15 03:41:33
Proponuję jeszcze inną opcję dla iOS 5.0 i nowszych:
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setFont:[UIFont systemFontOfSize:14]];
Na iOS 8 (podlinkowany przez Mike ' a Gledhilla):
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setDefaultTextAttributes:@{
NSFontAttributeName: [UIFont fontWithName:@"Helvetica" size:20],
}];
Dla iOS 9 i nowszych:
[[UITextField appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setDefaultTextAttributes:@{NSFontAttributeName: [UIFont fontWithName:@"Helvetica" size:20]}];
W ten sposób nie musisz mieszać z wyliczaniem podglądy dla każdego paska wyszukiwania w aplikacji.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-06-25 18:08:08
Bezpieczny sposób wykonania tej operacji jest następujący:
for(UIView *subView in searchBar.subviews) {
if ([subView isKindOfClass:[UITextField class]]) {
UITextField *searchField = (UITextField *)subView;
searchField.font = [UIFont fontWithName:@"Oswald" size:11];
}
}
Dlaczego jest to bezpieczniejsze od zaakceptowanej odpowiedzi? Ponieważ nie polega na tym, że indeks UITextField pozostaje stały. (jest to również środek czyszczący dla pętli)Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-05-25 11:40:30
Możesz użyć KVC (Key-Value Coding), aby uzyskać pole tekstowe
UITextField *textField = [self.searchBar valueForKey: @"_searchField"];
[textField setTextColor:[UIColor redColor]];
[textField setFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:17.0]];
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-01-25 12:10:22
Przyjęta odpowiedź nie jest zalecanym sposobem stosowania czcionki dla paska UISearchBar. Lepiej unikać tego podejścia i korzystać z odpowiedzi udzielonych przez @ rafalkitta lub @ josema.
Ale bądź ostrożny, zostanie to zastosowane do wszystkich pasków wyszukiwania przez aplikację. Aby zastosować to podejście do konkretnego paska wyszukiwania: Utwórz podklasę UISearchBar
i ustaw {[3] } na UITextField
Jak poniżej
Swift4 :
let defaultTextAttribs = [NSAttributedStringKey.font.rawValue: UIFont.boldSystemFont(ofSize: 21.0), NSAttributedStringKey.foregroundColor.rawValue:UIColor.red]
UITextField.appearance(whenContainedInInstancesOf: [CustomSearchBar.self]).defaultTextAttributes = defaultTextAttribs
Obj-C (iOS11)
UIFont *font = [UIFont boldSystemFontOfSize:21.0];
UIColor *color = [UIColor redColor];
NSDictionary * defaultTextAttribs = @{NSFontAttributeName:font, NSForegroundColorAttributeName: color};
[UITextField appearanceWhenContainedInInstancesOfClasses:@[[CustomSearchBar class]]].defaultTextAttributes = defaultTextAttribs;
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-10-10 10:55:07
W swift 2.0 Dla paska wyszukiwania stworzonego programowo można to zrobić:
override func layoutSubviews() {
super.layoutSubviews()
let textFieldInsideSearchBar = self.searchBar.valueForKey("searchField") as! UITextField
textFieldInsideSearchBar.font = UIFont.systemFontOfSize(20)
}
W tym przypadku umieściłem kod w podklasie UIView, ale będzie działał również w innych miejscach (np. viewWillAppear z UIViewController)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-10-16 17:09:07
W swift 3 możesz to osiągnąć poprzez:
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).font = UIFont(name: "AvenirNextCondensed-Regular", size: 17.0)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-01-13 09:11:50
UITextField
wygląd nie działa dla UISearchBar
S tworzonych programowo. Musisz użyć rekurencyjnego obejścia
- (void)viewDidLoad {
[super viewDidLoad];
[self recursivelySkinTextField:self.searchBar];
}
- (void)recursivelySkinTextField:(UIView *)view {
if (!view.subviews.count) return;
for (UIView *subview in view.subviews) {
if ([subview isKindOfClass:[UITextField class]]) {
UITextField *searchField = (UITextField *)subview;
searchField.font = [[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] font];
searchField.textColor = [[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] textColor];
}
[self recursivelySkinTextField:subview];
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-02-15 12:00:28
Chciałem użyć 'Roboto-Regular' jako domyślnej czcionki dla wszystkich pasków uisearchbars w moim projekcie. Zrobiłem rozszerzenie uisearchbar.
extension UISearchBar {
func addRobotoFontToSearchBar(targetSearchBar:UISearchBar?) -> UISearchBar
{
let textFieldInsideSearchBar = targetSearchBar!.valueForKey("searchField") as! UITextField
textFieldInsideSearchBar.font = UIFont(name: "Roboto-Regular", size: 15.0)
//UIBarButtons font in searchbar
let uiBarButtonAttributes: NSDictionary = [NSFontAttributeName: UIFont(name: "Roboto-Regular", size: 15.0)!]
UIBarButtonItem.appearance().setTitleTextAttributes(uiBarButtonAttributes as? [String : AnyObject], forState: UIControlState.Normal)
return targetSearchBar!
}
}
Użycie:
self.sampleSearchBar.addRobotoFontToSearchBar(sampleSearchBar)
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-05-27 07:03:22
Nie powinieneś używać prywatnego API jak w innych odpowiedziach. Jeśli chcesz dostosować paski wyszukiwania w całej aplikacji, możesz użyć proxy wyglądu klasy. Umożliwia modyfikację atrybutów tekstowych paska wyszukiwania, atrybutów zastępczych, przycisku anulowania, obrazu tła pola tekstowego, obrazu tła itp. Przykład użycia:
if #available(iOS 9.0, *) {
let searchBarTextAttributes = [
NSFontAttributeName: UIFont.boldSystemFont(ofSize: 12),
NSForegroundColorAttributeName: UIColor.red
]
// Default attributes for search text
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = searchBarTextAttributes
// Attributed placeholder string
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).attributedPlaceholder = NSAttributedString(string: "Search...", attributes: searchBarTextAttributes)
// Search bar cancel button
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes(searchBarTextAttributes, for: .normal)
UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).title = "Cancel"
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-04-07 13:47:43
Przepisał rekurencyjne rozwiązanie Eugene ' a w języku swift-zmienił nazwę funkcji, aby dokładniej opisać funkcję. W moim przypadku musiałem zmienić rozmiar czcionki. To mi pomogło.
func findAndSetTextField(view: UIView)
{
if (view.subviews.count == 0)
{
return
}
for var i = 0; i < view.subviews.count; i++
{
var subview : UIView = view.subviews[i] as UIView
if (subview.isKindOfClass(UITextField))
{
var searchField : UITextField = subview as UITextField
searchField.font = UIFont(name: "Helvetica", size: 19.0)!
}
findAndSetTextField(subview)
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-01-05 19:49:10
Spróbuj znaleźć pasek wyszukiwania po jego kluczu:
UITextField *searchField = [self.searchDisplayController.searchBar valueForKey:@"_searchField"];
searchField.font = [[UIFont fontWithName:@"Oswald" size:11];
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-11-18 04:37:40
Pełny kod powinien być poniżej:
for (UIView *v in (SYSTEM_VERSION_LESS_THAN(@"7.0")?searchBar.subviews:[[searchBar.subviews objectAtIndex:0] subviews])) {
if([v isKindOfClass:[UITextField class]]) {
UITextField *textField = (UITextField *)v;
[textField setFont:fREGULAR(@"15.0")];
return;
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-02-06 10:32:53
Dodaj poniższy kod, w którym zdefiniujesz lub skonfigurujesz Pasek UISearchBar
Swift4:
UILabel.appearance(whenContainedInInstancesOf: [UISearchBar.self]).font = UIFont.init(name: "Ubuntu-Regular", size: 16)
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).font = UIFont.init(name: "Ubuntu-Regular", size: 16)
Objective C:
[[UILabel appearanceWhenContainedIn:[UISearchBar class], nil] setFont:[UIFont fontWithName:@"Helvetica" size:12.0]];
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setFont:[UIFont fontWithName:@"Helvetica" size:12.0]];
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-04-13 13:11:20