更改 tabbaritem 的字体大小

发布于 2024-08-27 20:37:28 字数 22 浏览 5 评论 0原文

是否可以更改选项卡的字体大小?

Is it possible to change the font size of tabs?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

此岸叶落 2024-09-03 20:37:29

我推荐一个更好的方法:

[yourTabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
    [UIColor whiteColor], UITextAttributeTextColor, 
    [NSValue valueWithUIOffset:UIOffsetMake(0,0)], UITextAttributeTextShadowOffset, 
    [UIFont fontWithName:@"Helvetica" size:18.0], UITextAttributeFont, nil]
    forState:UIControlStateNormal];

I recommend a better way:

[yourTabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
    [UIColor whiteColor], UITextAttributeTextColor, 
    [NSValue valueWithUIOffset:UIOffsetMake(0,0)], UITextAttributeTextShadowOffset, 
    [UIFont fontWithName:@"Helvetica" size:18.0], UITextAttributeFont, nil]
    forState:UIControlStateNormal];
铜锣湾横着走 2024-09-03 20:37:29
for(UIViewController *tab in  self.tabBarController.viewControllers)

{        
  [tab.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
  [UIFont fontWithName:@"Helvetica" size:20.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];
}
听,心雨的声音 2024-09-03 20:37:29

在 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)
}

IN 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)

}

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)
}
迷途知返 2024-09-03 20:37:29

[更新] iOS 7.0+ 版本 @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];

主要变化是 UITextAttributeTextColor 和 UITextAttributeFont 均已弃用,

以便将其应用于所有选项卡(感谢 @ToolmakerSteve 指出)

for(UIViewController *tab in  self.tabBarController.viewControllers)
{        
    [tab.tabBarItem setTitleTextAttributes: ...];
}

[Update] iOS 7.0+ version of @cancer86's nice answer:

[[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];

The main change is that UITextAttributeTextColor and UITextAttributeFont are both deprecated

In order to apply it to all tabs (thanks to @ToolmakerSteve for pointing out)

for(UIViewController *tab in  self.tabBarController.viewControllers)
{        
    [tab.tabBarItem setTitleTextAttributes: ...];
}
智商已欠费 2024-09-03 20:37:29

在 iOS 5.0 或更高版本中很简单:

[[UITabBarItem appearance] setTitleTextAttributes:@{UITextAttributeFont:[UIFont boldSystemFontOfSize:15]} forState:UIControlStateNormal];

Simple in iOS 5.0 or later:

[[UITabBarItem appearance] setTitleTextAttributes:@{UITextAttributeFont:[UIFont boldSystemFontOfSize:15]} forState:UIControlStateNormal];
屋顶上的小猫咪 2024-09-03 20:37:29
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];
        }
    }
}
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];
        }
    }
}
月野兔 2024-09-03 20:37:29

我认为 Swift 中的这可以清晰地控制选项卡栏颜色和文本属性。

class MyTabBarController: UITabBarController {

  override func viewDidLoad() {
    super.viewDidLoad()
    tabBar.barTintColor = UIColor.green

    UITabBarItem.appearance().setTitleTextAttributes(
        [NSAttributedString.Key.font:UIFont.boldSystemFont(ofSize: 18), 
           NSAttributedString.Key.foregroundColor: UIColor.orange], for: .normal)
...             

I think this in Swift gives a clear control over the tab bar colors and text attributes.

class MyTabBarController: UITabBarController {

  override func viewDidLoad() {
    super.viewDidLoad()
    tabBar.barTintColor = UIColor.green

    UITabBarItem.appearance().setTitleTextAttributes(
        [NSAttributedString.Key.font:UIFont.boldSystemFont(ofSize: 18), 
           NSAttributedString.Key.foregroundColor: UIColor.orange], for: .normal)
...             
吃→可爱长大的 2024-09-03 20:37:29

[将其留在这里供我自己参考,只是对其他工作答案的即兴发挥。对于我来说,这是针对 iOS 7 的修复,这有点超出了问题......]

@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

缺少的方法是

#define IS_IOS7 ( UIDevice.currentDevice.systemVersion.floatValue > 6.9 )

[leaving this here for my own reference, just a riff off the other working answers. For mem it's a fix for iOS 7, which is beyond the question by a 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

the missing method is

#define IS_IOS7 ( UIDevice.currentDevice.systemVersion.floatValue > 6.9 )
尝蛊 2024-09-03 20:37:29

在 Swift 4 中

 override func viewDidLoad() {
        super.viewDidLoad()
        let appearance = UITabBarItem.appearance()
        let attributes: [NSAttributedString.Key: AnyObject] = [NSAttributedString.Key(rawValue: NSAttributedString.Key.font.rawValue):UIFont(name: "American Typewriter", size: 12)!, NSAttributedString.Key(rawValue: NSAttributedString.Key.foregroundColor.rawValue): UIColor.orange]
        appearance.setTitleTextAttributes(attributes, for: .normal)

    }

IN Swift 4

 override func viewDidLoad() {
        super.viewDidLoad()
        let appearance = UITabBarItem.appearance()
        let attributes: [NSAttributedString.Key: AnyObject] = [NSAttributedString.Key(rawValue: NSAttributedString.Key.font.rawValue):UIFont(name: "American Typewriter", size: 12)!, NSAttributedString.Key(rawValue: NSAttributedString.Key.foregroundColor.rawValue): UIColor.orange]
        appearance.setTitleTextAttributes(attributes, for: .normal)

    }
折戟 2024-09-03 20:37:29

其实是有办法的。

    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]];
            }
        }
    }
}

Actually there is a way.

    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]];
            }
        }
    }
}
时常饿 2024-09-03 20:37:29

Xcode 13 和 Swift 5+

您可以从自定义扩展类更改选项卡栏项目字体属性。

例如,

  1. 创建一个扩展文件 (.swift) 并放置以下代码

import UIKit

@IBDesignable
extension UITabBarItem {

@IBInspectable var fontSize : CGFloat{
    set{
        if(newValue > 0){
            let barItemFontAttributes = [NSAttributedString.Key.font : UIFont(name: "Avenir", size: newValue)]
            self.setTitleTextAttributes(barItemFontAttributes as [NSAttributedString.Key : Any], for: .normal)
        }
    }
    get{
        return self.fontSize
    }
}
  1. 更改字体大小

现在您可以从属性检查器在此处输入图片描述

如果有更好的方法请指出!

Xcode 13 and Swift 5+

you can change tab bar item font attributes from the custom extension class.

for example,

  1. create an extension file (.swift) and place the following code

import UIKit

@IBDesignable
extension UITabBarItem {

@IBInspectable var fontSize : CGFloat{
    set{
        if(newValue > 0){
            let barItemFontAttributes = [NSAttributedString.Key.font : UIFont(name: "Avenir", size: newValue)]
            self.setTitleTextAttributes(barItemFontAttributes as [NSAttributedString.Key : Any], for: .normal)
        }
    }
    get{
        return self.fontSize
    }
}
  1. now you can change the font size from attribute inspector

enter image description here

if there is a better way please mention it!

打小就很酷 2024-09-03 20:37:29

你必须从ios 13开始改变这样做的方式

    let attributesSelected: [NSAttributedString.Key: AnyObject] = [NSAttributedString.Key(rawValue: NSAttributedString.Key.font.rawValue):UIFont(name: "Montserrat-Bold", size: 13)!, NSAttributedString.Key(rawValue: NSAttributedString.Key.foregroundColor.rawValue): UIColor.primary]
    
    let attributesDefault: [NSAttributedString.Key: AnyObject] = [NSAttributedString.Key(rawValue: NSAttributedString.Key.font.rawValue):UIFont(name: "Montserrat-Bold", size: 13)!]
    
    if #available(iOS 13.0, *) {
        
        let tabBarAppearance = UITabBarAppearance()
        let tabBarItemAppearance = UITabBarItemAppearance()
        tabBarItemAppearance.selected.titleTextAttributes = attributesSelected
        tabBarItemAppearance.normal.titleTextAttributes = attributesDefault
        tabBarAppearance.stackedLayoutAppearance = tabBarItemAppearance
        tabBar.standardAppearance = tabBarAppearance
        
    } else {
               
        UITabBarItem.appearance().setTitleTextAttributes(
            attributesSelected, for: .selected)
        UITabBarItem.appearance().setTitleTextAttributes(
            attributesDefault, for: .normal)
    }

You must change the way to do it from ios 13

    let attributesSelected: [NSAttributedString.Key: AnyObject] = [NSAttributedString.Key(rawValue: NSAttributedString.Key.font.rawValue):UIFont(name: "Montserrat-Bold", size: 13)!, NSAttributedString.Key(rawValue: NSAttributedString.Key.foregroundColor.rawValue): UIColor.primary]
    
    let attributesDefault: [NSAttributedString.Key: AnyObject] = [NSAttributedString.Key(rawValue: NSAttributedString.Key.font.rawValue):UIFont(name: "Montserrat-Bold", size: 13)!]
    
    if #available(iOS 13.0, *) {
        
        let tabBarAppearance = UITabBarAppearance()
        let tabBarItemAppearance = UITabBarItemAppearance()
        tabBarItemAppearance.selected.titleTextAttributes = attributesSelected
        tabBarItemAppearance.normal.titleTextAttributes = attributesDefault
        tabBarAppearance.stackedLayoutAppearance = tabBarItemAppearance
        tabBar.standardAppearance = tabBarAppearance
        
    } else {
               
        UITabBarItem.appearance().setTitleTextAttributes(
            attributesSelected, for: .selected)
        UITabBarItem.appearance().setTitleTextAttributes(
            attributesDefault, for: .normal)
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文