Est-il possible de modifier la taille de police des onglets?Modification de la taille de la police de tabulation
Répondre
En fait, il y a un moyen.
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];
}
}
}
Je recommande une meilleure façon:
[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];
}
MISE À JOUR: Voir la réponse de voghDev pour la version iOS 7.0+, qui évite l'obsolète UITextAttributeFont. – ToolmakerSteve
[en laissant ce ici pour ma propre référence, juste un riff au large des autres réponses travail. Pour mem il est un correctif pour iOS 7, qui est au-delà de la question par un peu ...]
@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
la méthode manquante est
#define IS_IOS7 (UIDevice.currentDevice.systemVersion.floatValue > 6.9)
Simple dans iOS 5.0 ou version ultérieure:
[[UITabBarItem appearance] setTitleTextAttributes:@{UITextAttributeFont:[UIFont boldSystemFontOfSize:15]} forState:UIControlStateNormal];
bonne solution, mais utilisez 'NSFontAttributeName' au lieu de' UITextAttributeFont' – Laszlo
[Mise à jour] La version iOS 7.0+ de belle réponse @ 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];
Le principal changement est que UITextAttributeTextColor et UITextAttributeFont sont tous deux déconseillés
Pour l'appliquer à tous les onglets (grâce à @ToolmakerSteve d'avoir signalé)
for(UIViewController *tab in self.tabBarController.viewControllers)
{
[tab.tabBarItem setTitleTextAttributes: ...];
}
.. pour appliquer à tous les onglets, à partir de la réponse de spatil 'pour (onglet UIViewController * dans self.tabBarController.viewControllers) [tab.tabBarItem setTitleTextAttributes: ...' – ToolmakerSteve
ok réponse mise à jour – voghDev
pouvons-nous avoir la version 3 rapide pour cela –
Swift 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)
}
à 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)
}
Il ne fonctionne que dans iOS5.0 (ou plus tard) .. – Kjuly
agréable, ses œuvres tout à fait bien pour moi .. merci – Gaurav
Quelle place avez-vous besoin d'écrire ci-dessus morceau de code? J'essaie de l'utiliser après [self.tabBarController setViewControllers: aControllerList animé: YES]; mais cela n'aide pas. – Abhinav