在 Swift for iOS 中将 TabBarItem 标题字体更改为粗体

Change TabBarItem title font to bold in Swift for iOS(在 Swift for iOS 中将 TabBarItem 标题字体更改为粗体)

本文介绍了在 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 文件分配给您的身份检查器"下的 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 your UITabBarController under "Identity Inspector."

我们需要在 UITabBarController 中自定义逻辑.

<小时>

  1. 创建一个新的 UITabBarItem 子类,将相同的文件分配给您的所有 UITabBarItems:文件 -> 新文件 -> 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)
        }

    }

}

这里我们设置了初始状态,以便在应用打开时正确设置选项卡,我们将在下一个子类中处理物理的选项卡按下.

  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 标题字体更改为粗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在 Swift for iOS 中将 TabBarItem 标题字体更改为粗体

基础教程推荐