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

前端之家收集整理的这篇文章主要介绍了在Swift for iOS中将TabBarItem标题字体更改为粗体前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试将所选标签栏项的字体粗细设置为粗体字体.似乎它没有任何效果.知道什么是错的. forState:.Normal按预期工作,对于State:.Selected无效.
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)

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

这为我们稍后设置,以确定按下哪个选项卡.

>创建一个新的UITabBarController子类,然后分配它:FILE – >新文件 – > iOS Cocoa Touch – >创建一个UITabBarController的子类.将新的.swift文件分配给您的.swift文件
“Identity Inspector”下的UITabBarController.

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

>创建UITabBarItem的新子类,将相同的文件分配给所有UITabBarItem:FILE – >新文件 – > iOS Cocoa Touch – >创建一个UITabBarItem的子类,并为所有选项卡分配相同的子类.

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

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

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

    }

}

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

>将此代码添加到UITabBarController子类中,这是在按下选项卡时分配正确状态的逻辑.

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,for: UIControlState.normal)
            } else {
                tabs?[x].setTitleTextAttributes([NSFontAttributeName: normalTitleFont,for: UIControlState.normal)
            }

            x += 1

        }

    }

}

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

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

希望这有帮助!

原文链接:https://www.f2er.com/swift/320018.html

猜你在找的Swift相关文章