-1

I'm using the following code to highlight the selected tab title.

  [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName,nil] forState:UIControlStateNormal];

  [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor redColor], NSForegroundColorAttributeName, nil] forState:UIControlStateSelected];

The above code works perfectly. Can I modify the above code to increase the font size of the tab title and make it center ?

Santo
  • 1,611
  • 3
  • 17
  • 37

3 Answers3

0

This Code will help you.

// set the text color for selected state
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor blackColor], NSForegroundColorAttributeName,[UIFont fontWithName:YOUR_FONT_NAME size:YOUR_FONT_SIZE],NSFontAttributeName ,nil] forState:UIControlStateSelected];

// set the text color for unselected state
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor blackColor], NSForegroundColorAttributeName,[UIFont fontWithName:YOUR_FONT_NAME size:YOUR_FONT_SIZE],NSFontAttributeName ,nil] forState:UIControlStateNormal];


//To make title center use below one 
[[self.tabBarController.tabBar.items objectAtIndex:0] setTitlePositionAdjustment:UIOffsetMake(0, 0)];
[[self.tabBarController.tabBar.items objectAtIndex:1] setTitlePositionAdjustment:UIOffsetMake(0, 0)];

//if you have more tabs increase the index
Santo
  • 1,611
  • 3
  • 17
  • 37
0

Try this:

UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
tabBarController.selectedIndex = 1;
UITabBar *tabBar = tabBarController.tabBar;
UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1];
UITabBarItem *tabBarItem3 = [tabBar.items objectAtIndex:3];
UITabBarItem *tabBarItem4 = [tabBar.items objectAtIndex:4];
tabBarItem1.title = @"Tab1"; tabBarItem2.title = @"Tab2";
tabBarItem3.title = @"Tab3"; tabBarItem4.title = @"Tab4";
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: [UIColor whiteColor], NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
UIColor *titleHighlightedColor = [UIColor colorWithRed:153/255.0 green:192/255.0 blue:48/255.0 alpha:1.0];
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: titleHighlightedColor, NSForegroundColorAttributeName, nil] forState:UIControlStateHighlighted]; return YES;
neha
  • 167
  • 2
  • 11
0

This solution works for me

let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.alignment = .center
  
  if #available(iOS 13, *) {
     let appearance = tabBarController.tabBar.standardAppearance
     appearance.stackedLayoutAppearance.normal.titleTextAttributes = [
        NSAttributedString.Key.paragraphStyle: paragraphStyle
     ]
     appearance.stackedLayoutAppearance.selected.titleTextAttributes = [
        NSAttributedString.Key.paragraphStyle: paragraphStyle
     ]
  } else {
     if #available(iOS 11, *) {
        UITabBarItem.appearance().setTitleTextAttributes([
           NSAttributedString.Key.paragraphStyle: paragraphStyle
        ], for: .normal)
        UITabBarItem.appearance().setTitleTextAttributes([
           NSAttributedString.Key.paragraphStyle: paragraphStyle
        ], for: .selected)
     }
  }
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
ArturRuz
  • 49
  • 1
  • 5