【问题标题】:Custom Font in Tabbar标签栏中的自定义字体
【发布时间】:2011-09-06 13:22:40
【问题描述】:

嘿,
有没有办法将标签栏的字体设置为例如黑板?我已经看到字体大小的问题并尝试使用字体,但循环不起作用。

如果无法自定义字体,是否可以删除所有文本并使标签栏的项目覆盖整个标签?或者我可以以某种方式将 imageView 放在标签栏的顶部并使用标签“通过”该图像?

谢谢!

【问题讨论】:

  • 也许最好解释一下您的确切意图,因为您不完全清楚您想要实现什么。一般来说,不建议子类化/覆盖 UITabBar 及其项目,但也许有一些更好的方法来制作你想要的东西 - 告诉我们它是什么。
  • @Matthes 我只需要标签栏的自定义外观。 “最简单”的自定义是让标签栏以黑板字体显示它的文本。 “最好的”是如果我可以让每个标签都被图像“覆盖”。不是图标,而是覆盖整个选项卡的图像,就像按钮的背景图像一样。这可能吗?

标签: iphone objective-c fonts uitabbarcontroller


【解决方案1】:

我发现自己是一个了不起且非常简单的教程,可以完全满足我的需要。 对于进一步的参考和所有,这可能也需要,这里是链接

Part 1

Part 2

【讨论】:

  • 尽量不要发布链接答案。如果链接断开,那么接受的答案突然变得毫无用处。而是尝试发布链接的基本内容,然后将链接作为附加内容发布以增强您的答案
【解决方案2】:

放到AppDelegate.m下:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

代码如下:

    NSMutableDictionary *attributes = [NSMutableDictionary dictionaryWithDictionary: [[UITabBarItem appearance] titleTextAttributesForState:UIControlStateNormal]];
[attributes setValue:[UIFont fontWithName:@"Avenir" size:10] forKey:UITextAttributeFont];
[[UITabBarItem appearance] setTitleTextAttributes:attributes1 forState:UIControlStateNormal];

【讨论】:

    【解决方案3】:

    在 iOS 13 上,UITabBarItem.appearance().setTitleTextAttributes 方法存在一个错误,所以我们需要做这样的解决方法:

        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.alignment = .center
        let attributes = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 12),
                          NSAttributedString.Key.paragraphStyle: paragraphStyle]
    
        let appearance = UITabBarItem.appearance()
        appearance.setTitleTextAttributes(attributes, for: .normal)
    
        if #available(iOS 13.0, *) {
            let appearance = UITabBarAppearance()
            appearance.stackedLayoutAppearance.normal.titleTextAttributes = attributes
            appearance.stackedLayoutAppearance.normal.badgeBackgroundColor = .blue
            appearance.stackedLayoutAppearance.selected.titleTextAttributes = attributes
            appearance.stackedLayoutAppearance.selected.badgeBackgroundColor = .blue
            tabBar.standardAppearance = appearance
        }
    

    如果你只在 iOS 13 上设置外观 tabBar.standardAppearance = appearance 你仍然会有一个错误,但是另一个

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-02-24
      • 2021-05-11
      • 2017-04-17
      • 1970-01-01
      • 2014-05-23
      • 2011-04-24
      • 1970-01-01
      相关资源
      最近更新 更多