使用 UITabBarItem 外观使选项卡标题居中
Centre the tab titles using UITabBarItem appearance
我正在使用以下代码来突出显示所选的标签标题。
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName,nil] forState:UIControlStateNormal];
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor redColor], NSForegroundColorAttributeName, nil] forState:UIControlStateSelected];
以上代码完美运行。我可以修改上面的代码来增加选项卡标题的字体大小并使其居中吗?
此代码将为您提供帮助。
// 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
试试这个:
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;
这个解决方案对我有用
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)
}
}
我正在使用以下代码来突出显示所选的标签标题。
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor], NSForegroundColorAttributeName,nil] forState:UIControlStateNormal];
[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor redColor], NSForegroundColorAttributeName, nil] forState:UIControlStateSelected];
以上代码完美运行。我可以修改上面的代码来增加选项卡标题的字体大小并使其居中吗?
此代码将为您提供帮助。
// 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
试试这个:
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;
这个解决方案对我有用
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)
}
}