Czy można zmienić rozmiar czcionki kart?Zmiana rozmiaru czcionki tabbaritem
Odpowiedz
Właściwie jest sposób.
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];
}
}
}
polecam lepszy sposób:
[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];
}
AKTUALIZACJA: Zobacz odpowiedź voghDev dla wersji iOS 7.0+, która omija przestarzałe UITextAttributeFont. – ToolmakerSteve
[pozostawiając to tutaj dla własnego odniesienia, tylko riff off innych odpowiedzi roboczych. Dla mem jest to poprawka dla iOS 7, który jest poza pytaniem o trochę ...]
@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
brakującym metoda jest
#define IS_IOS7 (UIDevice.currentDevice.systemVersion.floatValue > 6.9)
Prosty w iOS 5.0 lub nowszym:
[[UITabBarItem appearance] setTitleTextAttributes:@{UITextAttributeFont:[UIFont boldSystemFontOfSize:15]} forState:UIControlStateNormal];
dobre rozwiązanie, ale użyj 'NSFontAttributeName' zamiast' UITextAttributeFont' – Laszlo
[Aktualizacja] iOS 7.0+ wersja miłą odpowiedź @ cancer86 za:
[[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];
Główną zmianą jest to, że UITextAttributeTextColor i UITextAttributeFont są zarówno przestarzała
Aby zastosować go do wszystkich kartach (dzięki @ToolmakerSteve za wskazanie)
for(UIViewController *tab in self.tabBarController.viewControllers)
{
[tab.tabBarItem setTitleTextAttributes: ...];
}
.. aby zastosować do wszystkich kart, z odpowiedzi spatil 'dla (karta UIViewController * w self.tabBarController.viewControllers) [tab.tabBarItem setTitleTextAttributes: ...' – ToolmakerSteve
Odpowiedź ok zaktualizowana – voghDev
Czy możemy mieć szybką wersję 3 do tego –
w 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)
}
W 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)
}
To działa tylko w iOS5.0 (lub nowszy) .. – Kjuly
ładne, jego prace absolutnie w porządku dla mnie .. dzięki – Gaurav
Które miejsce zrobić trzeba napisać wyżej kawałek kodu? Próbuję go używać po [self.tabBarController setViewControllers: aControllerList animated: YES]; ale to nie pomaga. – Abhinav