本文介绍了在Swift for iOS中将TabBarItem标题字体更改为粗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将所选标签栏项的字体粗细设置为粗体字体。似乎它没有任何效果。知道什么是错的。 forState:.Normal 按预期工作, forState:.Selected 无效。

I'm trying to set the font weight of a selected tab bar item to bold font. It seems as it has no effect. Any idea what is wrong. forState: .Normal works as expected, forState: .Selected has no effect.

let tabBarItem0 = tabBar.items![0] as! UITabBarItem
var selectedImage0 : UIImage = UIImage(named:"ic_tabbar_item_one")!.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
var fontLight:UIFont = UIFont(name: "HelveticaNeue-UltraLight", size: 12)!
var fontBold:UIFont = UIFont(name: "HelveticaNeue-Bold", size: 12)!

tabBarItem0.image = unselectedImage0
tabBarItem0.selectedImage = selectedImage0
tabBarItem0.title = "Overview"
tabBarItem0.setTitleTextAttributes(
    [
        NSForegroundColorAttributeName: UIColor.whiteColor(),
        NSFontAttributeName: fontLight
    ], forState: .Normal)

tabBarItem0.setTitleTextAttributes(
    [
        NSForegroundColorAttributeName: UIColor.whiteColor(),
        NSFontAttributeName: fontBold
    ], forState: UIControlState.Selected)


推荐答案

找到解决方案(Swift 3,XCode 8.1)



FOUND THE SOLUTION (Swift 3, XCode 8.1)


  1. 在Storyboard中,为每个UITabBarItem提供一个唯一的标记:对于每个标签 - >选择它并转到它的属性检查器 - >给每个标签唯一标记字段中的数字,但您不应使用零(我使用1到4)。

  1. In Storyboard, give a unique Tag to each UITabBarItem you have: For every tab -> Select it and go to it's "Attributes Inspector" -> Give each one a unique number in the "Tag" field but you should not use zero (I used 1 through 4).

这设置了你稍后,确定按哪个标签。








  1. 创建UITabBarController的新子类然后分配它:文件 - >新文件 - > iOS Cocoa Touch - >创建UITabBarController的子类。将新的.swift文件分配给Identity Inspector下的
    UITabBarController。

  1. Create a new subclass of UITabBarController and then assign it: FILE -> New File -> iOS Cocoa Touch -> create a Subclass of UITabBarController. Assign the new .swift file to yourUITabBarController under "Identity Inspector."

我们需要在UITabBarController中使用自定义逻辑。








  1. 创建UITabBarItem的新子类,将相同的文件分配给所有UITabBarItems :FILE - >新建文件 - > iOS Cocoa Touch - >创建UITabBarItem的子类并将相同的文件分配给所有标签。

  1. Create a new subclass of UITabBarItem, assign the same file to all of your UITabBarItems: FILE -> New File -> iOS Cocoa Touch -> create a Subclass of UITabBarItem and assign the same one to all of your tabs.

我们的标签栏项目中需要共享的自定义逻辑。








  1. 将此代码添加到您的UITabBarItem子类,它设置初始状态(主要选项卡粗体,其余未选中),并允许程序化选项卡更改:

  1. Add this code to your UITabBarItem subclass, it sets up the initial state (primary tab bold, the rest unselected) and will allow for programmatic tab changes as well:

class MyUITabBarItemSubclass: UITabBarItem {

    //choose initial state fonts and weights here
    let normalTitleFont = UIFont.systemFont(ofSize: 12, weight: UIFontWeightRegular)
    let selectedTitleFont = UIFont.systemFont(ofSize: 12, weight: UIFontWeightBold)

    //choose initial state colors here
    let normalTitleColor = UIColor.gray
    let selectedTitleColor = UIColor.black

    //assigns the proper initial state logic when each tab instantiates
    override func awakeFromNib() {
        super.awakeFromNib()

        //this tag # should be your primary tab's Tag*
        if self.tag == 1 {
            self.setTitleTextAttributes([NSFontAttributeName: selectedTitleFont, NSForegroundColorAttributeName: selectedTitleColor], for: UIControlState.normal)
        } else {
            self.setTitleTextAttributes([NSFontAttributeName: normalTitleFont, NSForegroundColorAttributeName: normalTitleColor], for: UIControlState.normal)
        }

    }

}


这里我们设置了initi al状态,以便在应用程序打开时正确设置选项卡,我们将在下一个子类中处理物理选项卡。


  1. 将此代码添加到您的UITabBarController子类,这是为您分配正确状态的逻辑按下选项卡。

  1. Add this code to your UITabBarController subclass, it's the logic for assigning the correct states as you press on the tabs.

class MyUITabBarControllerSubclass: UITabBarController {

    //choose normal and selected fonts here
    let normalTitleFont = UIFont.systemFont(ofSize: 12, weight: UIFontWeightRegular)
    let selectedTitleFont = UIFont.systemFont(ofSize: 12, weight: UIFontWeightBold)

    //choose normal and selected colors here
    let normalTitleColor = UIColor.gray
    let selectedTitleColor = UIColor.black


    //the following is a delegate method from the UITabBar protocol that's available
    //to UITabBarController automatically. It sends us information every
    //time a tab is pressed. Since we Tagged our tabs earlier, we'll know which one was pressed,
    //and pass that identifier into a function to set our button states for us

    override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        setButtonStates(itemTag: item.tag)
    }


    //the function takes the tabBar.tag as an Int
    func setButtonStates (itemTag: Int) {

        //making an array of all the tabs
        let tabs = self.tabBar.items

        //looping through and setting the states
        var x = 0
        while x < (tabs?.count)! {

            if tabs?[x].tag == itemTag {
                tabs?[x].setTitleTextAttributes([NSFontAttributeName: selectedTitleFont, NSForegroundColorAttributeName: selectedTitleColor], for: UIControlState.normal)
            } else {
                tabs?[x].setTitleTextAttributes([NSFontAttributeName: normalTitleFont, NSForegroundColorAttributeName: normalTitleColor], for: UIControlState.normal)
            }

            x += 1

        }

    }

}


看起来这很痛苦,因为由于某种原因,标签不会将状态变化识别为.Selected。我们必须通过仅使用.Normal状态来做所有事情 - 基本上自己检测状态变化。


  1. 您可以通过编程方式更改选项卡并仍然通过...检测状态更改。如果有人有兴趣,我稍后会更新,只需询问。

  1. You can programmatically change the tabs and still detect state changes by... I'll update this later if anyone has an interest, just ask.

希望这有帮助!

这篇关于在Swift for iOS中将TabBarItem标题字体更改为粗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 07:52