Ist es möglich, die Schriftgröße von Tabs zu ändern?Ändern der Schriftgröße von Tabaritem
Antwort
Eigentlich gibt es einen Weg.
NSMutableArray *tabBarItems = [[[[[self.view subviews] objectAtIndex:1] subviews] mutableCopy] autorelease];
for (int item = 0; item < [tabBarItems count]; item++) {
for (int subview = 0; subview < [[[tabBarItems objectAtIndex:item] subviews] count]; subview++) {
for (int item = 0; item < [tabBarItems count]; item++) {
for (int subview = 0; subview < [[[tabBarItems objectAtIndex:item] subviews] count]; subview++) {
if ([[[[tabBarItems objectAtIndex:item] subviews] objectAtIndex:subview] isKindOfClass:NSClassFromString(@"UITabBarButtonLabel")])
[[[[tabBarItems objectAtIndex:item] subviews] objectAtIndex:subview] setFont:[UIFont systemFontOfSize:6.0f]];
}
}
}
}
TabBarIncreaseFonts(self.tabBarController);
void TabBarIncreaseFonts(UITabBarController* customTabBarController)
{
for(UIView* controlLevelFirst in [customTabBarController.tabBar subviews])
{
if(![controlLevelFirst isKindOfClass:NSClassFromString(@"UITabBarButton")])
continue;
for(id controlLevelSecond in [controlLevelFirst subviews])
{
[controlLevelSecond setBounds: CGRectMake(0, 0, 100, 48)];
if(![controlLevelSecond isKindOfClass:NSClassFromString(@"UITabBarButtonLabel")])
continue;
[controlLevelSecond setFont: [UIFont boldSystemFontOfSize:20]];
[controlLevelSecond setFrame: CGRectMake(0, 0, 96, 48)];
[controlLevelSecond setTextAlignment:UITextAlignmentCenter];
}
}
}
Ich empfehle einen besseren Weg:
[yourTabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], UITextAttributeTextColor,
[NSValue valueWithUIOffset:UIOffsetMake(0,0)], UITextAttributeTextShadowOffset,
[UIFont fontWithName:@"Helvetica" size:18.0], UITextAttributeFont, nil]
forState:UIControlStateNormal];
for(UIViewController *tab in self.tabBarController.viewControllers)
{
[tab.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:@"Helvetica" size:20.0], UITextAttributeFont, nil]
forState:UIControlStateNormal];
}
UPDATE: Siehe voghDevs Antwort für iOS 7.0+ Version, die veraltet UITextAttributeFont vermeidet. – ToolmakerSteve
[dies hier für meine eigene Referenz zu verlassen, nur ein Riff aus den anderen Arbeits Antworten. Für mem ist es ein Update für iOS 7, die über die Frage durch ein Bit ...]
@implementation UITabBarController (Util)
- (void) fixForIos7 {
if (!IS_IOS7)
return;
UIFont *tabBarFont = [UIFont systemFontOfSize:12];
NSDictionary *titleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
tabBarFont, UITextAttributeFont, nil];
for(UIViewController *tab in self.viewControllers) {
[tab.tabBarItem setTitleTextAttributes:titleTextAttributes forState:UIControlStateNormal];
}
}
@end
die fehlende Methode ist
#define IS_IOS7 (UIDevice.currentDevice.systemVersion.floatValue > 6.9)
Einfach in iOS 5.0 oder höher:
[[UITabBarItem appearance] setTitleTextAttributes:@{UITextAttributeFont:[UIFont boldSystemFontOfSize:15]} forState:UIControlStateNormal];
gute Lösung, aber verwenden Sie 'NSFontAttributeName' anstelle von' UITextAttributeFont' – Laszlo
[Update] iOS 7.0+ Version von nette Antwort des @ cancer86:
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], NSForegroundColorAttributeName,
[UIFont fontWithName:@"Helvetica" size:tabFontSize],
NSFontAttributeName,
nil] forState:UIControlStateNormal];
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor redColor], NSForegroundColorAttributeName,
[UIFont fontWithName:@"Helvetica" size:tabFontSize], NSFontAttributeName,
nil] forState:UIControlStateSelected];
Die wichtigste Änderung ist, dass UITextAttributeTextColor und UITextAttributeFont beide
veraltetUm es auf allen Registerkarten
for(UIViewController *tab in self.tabBarController.viewControllers)
{
[tab.tabBarItem setTitleTextAttributes: ...];
}
.. auf alle Registerkarten, aus spatls Antwort 'für (Registerkarte UIViewController * in self.tabBarController.viewControllers) [tab.tabBarItem setTitleTextAttributes: ...' – ToolmakerSteve
ok Antwort aktualisiert – voghDev
können wir schnelle 3-Version dafür haben –
IN Swift (Danke für den Hinweis auf @ToolmakerSteve) 2,0
override func viewDidLoad() {
super.viewDidLoad()
let appearance = UITabBarItem.appearance()
let attributes: [String: AnyObject] = [NSFontAttributeName:UIFont(name: "American Typewriter", size: 12)!, NSForegroundColorAttributeName: UIColor.orangeColor()]
appearance.setTitleTextAttributes(attributes, forState: .Normal)
}
In Swift 3.0
override func viewDidLoad() {
super.viewDidLoad()
let appearance = UITabBarItem.appearance()
let attributes: [String: AnyObject] = [NSFontAttributeName:UIFont(name: "American Typewriter", size: 12)!, NSForegroundColorAttributeName: UIColor.orange]
appearance.setTitleTextAttributes(attributes, for: .normal)
}
Es funktioniert nur in iOS5.0 (oder höher) .. – Kjuly
schön, seine Werke absolut gut für mich .. Dank – Gaurav
Welcher Ort tun müssen, um Sie über Stück Code schreiben? Ich versuche es nach [self.tabBarController setViewControllers: aControllerList animiert: YES]; aber das hilft nicht. – Abhinav